Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 实战java发邮件
【标  题】:实战java发邮件
【关键字】:java
【来  源】:http://www.cublog.cn/u/19296/showart.php?id=150100

实战java发邮件

Your Ad Here

必须下载sun公司的JavaMail API包,我用的jakarta-tomcat-5.0.18里面自己带的 activation.jar  mail.jar 没有的话可以去这个网站看看

http://java.sun.com/products/javamail

接着把2个jar包放到classpath下面即可

源程序如下,替换红色部分即可使用。

对您有帮助或有问题请来信讨论:thizlinuxkevin@sina.com

import java.util.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;

/**
* <p>Title: java mail</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author kevin
* @version 0.1
*/

public class SendMail
{

public SendMail()
{
}
public void send(){
try
{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host", "smtp.yourmail.com"); //smtp主机名。
props.put("mail.smtp.user","username@yourmail.com"); //发送方邮件地址。
props.put("mail.smtp.password","888888"); //邮件密码。
PopupAuthenticator popA=new PopupAuthenticator();//邮件安全认证。
PasswordAuthentication pop = popA.performCheck("username","888888"); //填写用户名及密码
sendMailSession = Session.getInstance(props, popA);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("username@yourmail.com"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("receiver@mail.com")); //接收方邮件地址
newMessage.setSubject("邮件主题");
newMessage.setSentDate(new Date());
String mailContent;
mailContent="你好!\n\n";
mailContent+="\t邮件正文\n\n";
mailContent+=new Date().toLocaleString();
newMessage.setText(mailContent); //邮件正文
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
{
SendMail sml = new SendMail();
sml.send();
}

public class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}

}
}

人事考勤薪资管理系统简介:【上一篇】
perl doc 文档分类索引:【下一篇】
【相关文章】
  • Java高级-12个最重要的J2EE最佳实践
  • 将内容复制到剪贴板 javascript 代码
  • 使用javascript的日期函数
  • 理解JavaScript的OOP编程
  • 一个验证身份证号码的javascript函数
  • Javascript 函数: CTRL+回车 提交表单 By shawl.qiu
  • Memory space manipulating in Java(Section two:Process Memory Model on AIX part two-Default Memory Mo...
  • 启动java到unix后台
  • Java访问Access的一般方法!
  • java读取EXCEL
  • 【随机文章】
  • 告诉你一个真实的Google
  • 《计算机算法设计与分析》教材推荐
  • 控制台下的远程管理利器PsTools
  • 传奇世界 传世打宝全攻略
  • DevExpress SpreadSheet FS v1.1.11 For D6,7
  • 454 oops, unable to write pipe and I can't auth
  • SCO推新版Unix 寄厚望
  • WLAN实现网络延伸
  • 第25回 准确报告软件缺陷
  • 协议处理
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.