ps:感谢狂神老哥
依赖
javax.mail 1.4.7 javax.activation activation 1.1.1
邮件发送
package com.chen;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;
//发送一封简单邮件
public class MailDemo01 {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty("mail.host","smtp.qq.com"); //设置qq邮件服务器
properties.setProperty("mail.transport.protocol","smtp");//设置邮件发宋协议
properties.setProperty("mail.smtp.auth","true");//需要验证用户名密码
//qq邮箱的话还需要设置ssl加密,下面代码
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable","true");
properties.put("mail.smtp.ssl.socketFactory",sf);
//javamail发送邮件的五个步骤
//1.创建定义整个应用程序所需要的环境信息的session对象
//qq才有的,
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("******@qq.com","授权码");//发件人邮箱名和授权码
}
});
//开启session的debug模式,可以查看程序发送email的运行状态
session.setDebug(true);
//2.通过session得到transport对象
Transport transport = session.getTransport();
//3.使用邮箱的用户名和授权码连接上邮件服务器
transport.connect("smtp.qq.com","*******@qq.com","授权码");
//4.创建邮件
//带附件的,直至封装成方法
MimeMessage mimeMessage = imageMail(session);
//5.发送邮件
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
//6.关闭连接
transport.close();
}
public static MimeMessage imageMail(Session session) throws MessagingException {
//消息的固定信息
MimeMessage mimeMessage = new MimeMessage(session);
//邮件发送人
mimeMessage.setFrom(new InternetAddress("*****@qq.com"));
//邮件接收人,可以同时发送给很多人,我们这里只发给自己;
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("*****@qq.com"));
mimeMessage.setSubject("我也不知道是个什么东西就发给你了"); //邮件主题
//图片
MimeBodyPart body1 = new MimeBodyPart();
body1.setDataHandler(new DataHandler(new FileDataSource("mail/src/1.png")));
body1.setContentID("1.png"); //图片设置ID
//文本
MimeBodyPart body2 = new MimeBodyPart();
body2.setContent("请注意,我不是广告","text/html;charset=utf-8");
//附件
MimeBodyPart body3 = new MimeBodyPart();
body3.setDataHandler(new DataHandler(new FileDataSource("mail/src/1.png")));
body3.setFileName("1.png"); //附件设置名字
MimeBodyPart body4 = new MimeBodyPart();
body4.setDataHandler(new DataHandler(new FileDataSource("mail/src/1.png")));
body4.setFileName(""); //附件设置名字
//拼装邮件正文内容
MimeMultipart multipart1 = new MimeMultipart();
multipart1.addBodyPart(body1);
multipart1.addBodyPart(body2);
multipart1.setSubType("related"); //1.文本和图片内嵌成功!
//new MimeBodyPart().setContent(multipart1); //将拼装好的正文内容设置为主体
MimeBodyPart contentText = new MimeBodyPart();
contentText.setContent(multipart1);
//拼接附件
MimeMultipart allFile =new MimeMultipart();
allFile.addBodyPart(body3); //附件
allFile.addBodyPart(body4); //附件
allFile.addBodyPart(contentText);//正文
allFile.setSubType("mixed"); //正文和附件都存在邮件中,所有类型设置为mixed;
//放到Message消息中
mimeMessage.setContent(allFile);
mimeMessage.saveChanges();//保存修改
return mimeMessage;
}
}



