主要步骤:
1、创建包含邮件服务器信息的网络连接信息的Session对象
根据Properties设置的环境信息,得到Session对象(环境信息)
Session session = Session.getDefaultInstance(props,null);
2、创建代表邮件内容的Message对象
创建Message对象,传入session得到
MimeMessage message = new MimeMessage(session);
再根据MimeMultipart和MimeBodyPart嵌套得到邮件内容
3、从Session对象中获得Transprort对象,并调用它的方法发送Message对象
由session得到Transprot对象(可以完成邮件发送细节)
Transport transport = session.getTransport();
Transport对象进行connect、send和最后close连接操作
具体代码如下:
public class HtmlMessageSender {
String protocol = "smtp";
String from = "kai_wei_zhang@163.com";
String to = "692774116@qq.com";
String subject = "Html 测试";
String body = "<a href=http://blog.csdn.net/kai_wei_zhang>我的csdn博客</a>";
public static void main(String args[])throws Exception{
String user = "test_smtp@163.com";
String pass = "123456";
String server = "smtp.163.com";
HtmlMessageSender sender = new HtmlMessageSender();
//创建Session
Session session = sender.createSession();
//创建Message
MimeMessage message = sender.createMessage(session);
//创建Transport
Transport transport = session.getTransport();
//下面的用transport对象操作
transport.connect(server, user, pass);//------------注释
// transport.connect();
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private MimeMessage createMessage(Session session) throws Exception{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("test");
MimeMultipart multipart = new MimeMultipart("mixed");
//添加html格式文本
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body,"text/html;charset=gb2312");
multipart.addBodyPart(htmlBodyPart);
//附件添加
MimeBodyPart attachBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("d:\\attach\\psb.jpg");
attachBodyPart.setDataHandler(new DataHandler(fds));
attachBodyPart.setFileName(fds.getName());
multipart.addBodyPart(attachBodyPart);
message.setContent(multipart);
return message;
}
private Session createSession() {
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
// props.setProperty("mail.smtp.host", "smtp.163.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtp.auth", "true");
// Session session = Session.getDefaultInstance(props,
// new Authenticator() {
// String user = "test_smtp@163.com";
// String pass = "123456";
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(user, pass);
// }
// });
Session session = Session.getDefaultInstance(props,null);//------------注释
return session;
}
}
注意点
1、得到Session类可以使用getInstance和getDefaultInstance两个静态方法,区别在于前者每次调用都返回一个新的Session对象,而后者则返回一个Session对象后,将把这个Session对象安装为默认的Session对象,以后每次调用getDefaultInstance方法都将返回这个默认的Session对象
2、发送Message有两个方法,sendMessage和send,前者是一个非静态的方法,并且sendMessage方法在发送邮件前不会自动调用Message.saveChanges()方法,而后者是静态方法,并且会先自动调用Message.saveChanges()方法
3、用户认证信息有两种方式:第一种是现在代码体现的;第二种是用Authenticator类实现,当代码把注释代码去掉注释,把代码后面存在"//--------------------注释"的注释掉,就是第二种方式了。用第二种方式比较灵活,其实代码里面可以把内部类抽取出来,这样就容易从外部获取用户和密码,动态的得到认证信息。而不是写死在代码里面
4、写该实现代码时可以使用倒推的方法,首先编写调用Transport.send ,由于send方法需要一个Message对象作为参数,所以要创建Message,而创建Message需要Session对象作为构造方法的参数,所以先需要Session对象,这样往回推就容易写了