import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMail {
private MimeMessage mimemessage;
private Session session;
private Message message;
public void Send() throws Exception{
Properties pros = new Properties();
pros.setProperty("mail.host", "smtp.163.com"); //设置邮箱服务器
pros.setProperty("mail.transport.protocol", "smtp"); //邮件协议
pros.setProperty("mail.smtp.auth", "true");
session = Session.getInstance(pros);
session.setDebug(true);
Transport ts = session.getTransport();
//连接邮箱,参数邮箱服务器,用户名,密码
ts.connect("smtp.163.com","username","password");
message = createMimeMessage(session);
ts.sendMessage(message, message.getAllRecipients());
System.out.println("邮件已发送");
ts.close();
}
public MimeMessage createMimeMessage(Session session){
mimemessage = new MimeMessage(session);
try {
mimemessage.setFrom(new InternetAddress("miao.yahong@163.com")); //设置发件人
mimemessage.setRecipient(Message.RecipientType.TO, new InternetAddress("miao.yahong@163.com")); //设置收件人
mimemessage.setSubject("test test"); //设置主题
/**
* Multipart是一个容器它转载多个body Part(正文、附件或内嵌资源)
* 常见的multipart类型有三种:
* multipart/mixed:附件。
* multipart/related:内嵌资源。
* multipart/alternative:纯文本与超文本共存。
*/
MimeMultipart allparts = new MimeMultipart(); //邮件容器,用于保存邮件所有部分
MimeMultipart attchs = new MimeMultipart(); //附件容器,用于保存邮件附件部分
MimeMultipart bodys = new MimeMultipart("related"); //内容容器,用于保存邮件正文部分
MimeMultipart images = new MimeMultipart(); //图片容器,用于保存图片
MimeBodyPart attchpart = new MimeBodyPart(); //邮件附件
MimeBodyPart bodypart = new MimeBodyPart(); //邮件内容
MimeBodyPart textpart = new MimeBodyPart(); //邮件文本部分
MimeBodyPart image = new MimeBodyPart(); //邮件图片部分
MimeBodyPart attch001 = new MimeBodyPart(); //附件001
MimeBodyPart attch002 = new MimeBodyPart(); //附件002
MimeBodyPart image001 = new MimeBodyPart(); //图片001
MimeBodyPart image002 = new MimeBodyPart(); //图片002
String attch001name = "附件1.txt";
String attch002name = "附件2.txt";
String attch001file = "E:\\attch001.txt";
String attch002file = "E:\\attch002.txt";
String image001file = "E:\\image001.png";
String image002file = "E:\\image002.png";
FileDataSource source001 = new FileDataSource(attch001file); //创建附件001的数据源
FileDataSource source002 = new FileDataSource(attch002file); //创建附件002的数据源
DataHandler handler001 = new DataHandler(source001);
DataHandler handler002 = new DataHandler(source002);
attch001.setDataHandler(handler001);
attch002.setDataHandler(handler002);
attch001.setFileName(attch001name);
attch002.setFileName(attch002name);
attchs.addBodyPart(attch001); //将附件1装入附件容器
attchs.addBodyPart(attch002); //将附件2装入附件容器
source001 = new FileDataSource(image001file);
source002 = new FileDataSource(image002file);
handler001 = new DataHandler(source001);
handler002 = new DataHandler(source002);
image001.setDataHandler(handler001);
image002.setDataHandler(handler002);
image001.setContentID("image001");
image002.setContentID("image002");
images.addBodyPart(image001);
images.addBodyPart(image002);
image.setContent(images);
//内嵌图片src=cid:contentID
String connent = "<a href=http://www.baidu.com>www.baidu.com</a><h1>第一张图片<h1><image ";
connent += "src=\"cid:image001\"/><br><h1>第二张图片<h1><image src=\"cid:image002\"/>";
System.out.println(connent);
textpart.setContent(connent,"text/html;charset=UTF-8");
attchpart.setContent(attchs);
bodys.addBodyPart(textpart);
bodys.addBodyPart(image);
bodypart.setContent(bodys);
allparts.addBodyPart(bodypart,0);
allparts.addBodyPart(attchpart);
mimemessage.setContent(allparts); //将邮件内容装入邮件对象
} catch (MessagingException e) {
e.printStackTrace();
}
return mimemessage;
}
public static void main(String[] args) throws Exception{
SendMail sendmail = new SendMail();
sendmail.Send();
}
}
Java代码发送邮件
最新推荐文章于 2023-11-14 21:24:45 发布