导入jar包:
package com.test.javamail;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class Demo2 {
/**
* @param args
*/
public static void main(String[] args) {
//配置参数
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
//搭建环境
Session session = Session.getInstance(props);
//设置debug模式
session.setDebug(true);
//配置session环境
//Session session = Session.getInstance(new Properties());
//设置邮件内容
MimeMessage msg = new MimeMessage(session);
try {
//发信者
msg.setFrom(new InternetAddress("发送者邮箱"));
//主题
msg.setSubject("带html的邮件的标题");
//回复
msg.setReplyTo(new Address[]{new InternetAddress("lili@126.com")});
//抄送
msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("何黎明") + " <aa@163.com>," + MimeUtility.encodeText("曹佳欣") + " <bb@163.com>"));
} catch (Exception e1) {
e1.printStackTrace();
}
//设置复杂邮件内容
MimeMultipart msgMultipart = new MimeMultipart("mixed");
try {
msg.setContent(msgMultipart);
} catch (MessagingException e) {
e.printStackTrace();
}
//设置复杂邮件内容的附件
MimeBodyPart attch1 = new MimeBodyPart();
MimeBodyPart attch2 = new MimeBodyPart();
//设置复杂文件的文本
MimeBodyPart content = new MimeBodyPart();
try {
//将附件添加到邮件内容中
msgMultipart.addBodyPart(attch1);
msgMultipart.addBodyPart(attch2);
//将文本添加到邮件内容中
msgMultipart.addBodyPart(content);
} catch (MessagingException e) {
e.printStackTrace();
}
//附件一
DataSource ds1 = new FileDataSource(
"D:\\Java培训.txt"
);
DataHandler dh1 = new DataHandler(ds1 );
try {
//添加附件
attch1.setDataHandler(dh1);
//设置附件名
attch1.setFileName(
MimeUtility.encodeText("java培训.txt")
);
} catch (Exception e) {
e.printStackTrace();
}
//附件二
DataSource ds2 = new FileDataSource(
"D:\\aaa.gif"
);
DataHandler dh2 = new DataHandler(ds2 );
try {
//添加附件
attch2.setDataHandler(dh2);
//设置附件名字
attch2.setFileName("logo.gif");
} catch (MessagingException e) {
e.printStackTrace();
}
//带html的邮件内容
MimeMultipart bodyMultipart = new MimeMultipart("related");
try {
//复合类型添加到邮件内容中
content.setContent(bodyMultipart);
MimeBodyPart htmlPart = new MimeBodyPart();
bodyMultipart.addBodyPart(htmlPart);
//html内容
htmlPart.setContent("这是一封带html的邮件<a href=\"http://www.baidu.com\"><img src=\"http://c.hiphotos.baidu.com/image/w%3D400/sign=c3cf8d0eba0e7bec23da02e11f2fb9fa/d009b3de9c82d1583671c0b0820a19d8bd3e42c4.jpg\"></a>"
, "text/html;charset=gbk");
msg.saveChanges();
} catch (Exception e) {
e.printStackTrace();
}
//发送邮件
Transport transport;
try {
transport = session.getTransport();
//连接服务器,发送者账户名,密码
transport.connect("smtp.qq.com", 25, "发送者邮箱", "密码");
//接收者邮箱
transport.sendMessage(msg,
new Address[]{new InternetAddress("接收者邮箱")});
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}