packagecc.mrbird.febs.common.utils;importcom.sun.mail.util.MailSSLSocketFactory;import javax.mail.*;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Paths;importjava.security.GeneralSecurityException;importjava.util.Date;importjava.util.List;importjava.util.Properties;/*** 邮件发送工具类。
* 以下邮件中的配置参数,请在实际环境中,根据需要采取合适的配置方式。
* 发送邮件依赖 com.sun.mail(1.6.1) 包、javax.mail(1.5.0-b01) 包。
* 如果使用 Idea 运行,请将这两个包(可以直接到Maven目录下面去找)添加到项目的 Libraries 里面(快捷键:Ctrl + Alt + Shift + S)
*
*@authorZebe*/
public classSendEmailUtil {/*** 发件人别名(可以为空)*/
private final static String fromAliasName = "*****";/*** 登录用户名*/
private final static String ACCOUNT = "****";/*** 登录密码*/
private final static String PASSWORD = "****";/*** 邮件服务器地址*/
//QQ企业邮箱:smtp.exmail.qq.com//网易企业邮箱:smtphz.ym.163.com
private final static String HOST = "smtp.ym.163.com";/*** 发信端口*/
//QQ企业邮箱:465//网易企业邮箱:994
private final static String PORT = "994";/*** 发信协议*/
private final static String PROTOCOL = "ssl";/*** 收件人*/
privateString to;/*** 收件人名称*/
privateString toName;/*** 主题*/
privateString subject;/*** 内容*/
privateString content;/*** 附件列表(可以为空)*/
private ListattachFileList;/*** 构造器
*
*@paramto 收件人
*@paramsubject 主题
*@paramcontent 内容
*@paramattachFileList 附件列表*/
public SendEmailUtil(String to,String toName, String subject, String content, ListattachFileList) {this.to =to;this.toName =toName;this.subject =subject;this.content =content;this.attachFileList =attachFileList;
}/*** 认证信息*/
static class MyAuthenticator extendsAuthenticator {/*** 用户名*/String username= null;/*** 密码*/String password= null;/*** 构造器
*
*@paramusername 用户名
*@parampassword 密码*/
publicMyAuthenticator(String username, String password) {this.username =username;this.password =password;
}
@OverrideprotectedPasswordAuthentication getPasswordAuthentication() {return newPasswordAuthentication(username, password);
}
}/*** 发送邮件*/
public voidsend() {//设置邮件属性
Properties prop = newProperties();
prop.setProperty("mail.transport.protocol", PROTOCOL);
prop.setProperty("mail.smtp.host", HOST);
prop.setProperty("mail.smtp.port", PORT);
prop.setProperty("mail.smtp.auth", "true");
MailSSLSocketFactory sslSocketFactory= null;try{
sslSocketFactory= newMailSSLSocketFactory();
sslSocketFactory.setTrustAllHosts(true);
}catch(GeneralSecurityException e1) {
e1.printStackTrace();
}if (sslSocketFactory == null) {
System.err.println("开启 MailSSLSocketFactory 失败");
}else{
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sslSocketFactory);//创建邮件会话(注意,如果要在一个进程中切换多个邮箱账号发信,应该用 Session.getInstance)
Session session = Session.getDefaultInstance(prop, newMyAuthenticator(ACCOUNT, PASSWORD));//开启调试模式(生产环境中请不要开启此项)
session.setDebug(true);try{
MimeMessage mimeMessage= newMimeMessage(session);//设置发件人别名(如果未设置别名就默认为发件人邮箱)
mimeMessage.setFrom(newInternetAddress(ACCOUNT, fromAliasName));//设置主题和收件人、发信时间等信息
mimeMessage.addRecipient(Message.RecipientType.TO, newInternetAddress(to,toName));
mimeMessage.setSubject(subject);
mimeMessage.setSentDate(newDate());//如果有附件信息,则添加附件
if (!attachFileList.isEmpty()) {
Multipart multipart= newMimeMultipart();
MimeBodyPart body= newMimeBodyPart();
body.setContent(content,"text/html; charset=UTF-8");
multipart.addBodyPart(body);//添加所有附件(添加时判断文件是否存在)
for(String filePath : attachFileList) {if(Files.exists(Paths.get(filePath))) {
MimeBodyPart tempBodyPart= newMimeBodyPart();
tempBodyPart.attachFile(filePath);
multipart.addBodyPart(tempBodyPart);
}
}
mimeMessage.setContent(multipart);
}else{
mimeMessage.setText(content);
}//开始发信
mimeMessage.saveChanges();
Transport.send(mimeMessage);
}catch (MessagingException |IOException e) {
e.printStackTrace();
}
}
}
}