工具类:
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
@Service
@PropertySource("classpath:mail.properties")
public class MailService {
@Autowired
private Environment env;
private static String match = "^[\w-]+(\.[\w-]+)@[\w-]+(\.[\w-]+)+$";
private int port; // 端口
private String server;// 邮件服务器
private String fromName; // 发送时显示的名字
private String user; // 发送的用户名email
private String password;// 密码
private Properties props = new Properties(); // 属性
private Session session; // 一次会话
private String[] cc;
private boolean isload = false;
private String SSL_FACTORY;
/*
*
* @param port
* 服务器端口号 入25
* @param server
* 邮件发送服务器 如:smtp.163.com
* @param fromName
* 发件人的名字 如:我
* @param user
* 邮箱账号 如:xxx@163.com
* @param password
* 邮箱密码
*/
public void init() {
if(isload){
return;
}
this.port = Integer.parseInt(env.getProperty("mail.port"));//25;
this.server = env.getProperty("mail.stmp");//"smtp.mxhichina.com";
this.fromName = env.getProperty("mail.from");
this.user = env.getProperty("mail.from");
this.password = env.getProperty("mail.password");
String otheremail = env.getProperty("cc");
if(!StringUtils.isEmpty(otheremail)){
cc = otheremail.split(",");
}
//
props.put("mail.smtp.host", server);
props.put("mail.smtp.socketFactory.port", String.valueOf(port));
props.put("mail.smtp.port", String.valueOf(port));
props.put("mail.smtp.auth", "true");
try {
// 得到一次会话
MailAuthenticator myauth = new MailAuthenticator(user, password);
session = Session.getDefaultInstance(props, myauth);
// 得到通道
//Transport transport = session.getTransport("smtp");
// 连接
//transport.connect(server, user, password);
} catch (Exception e) {
e.printStackTrace();
}
isload = true;
}
/**
* 发送邮件
*
* @param toEmail
* 目的地地址
* @param subject
* 邮件标题
* @param body
* 邮件正文
* @throws UnsupportedEncodingException
*/
public void sendEmail(String toEmail,String subject, String body) {
init();
try {
// 验证email
if (!toEmail.matches(match)) {
return;
}
// 发送的邮件地址
InternetAddress fromAddress = null;
InternetAddress[] toAddress = new InternetAddress[1];
// 创建消息
MimeMessage msg = new MimeMessage(session);
// 设置发送日期
msg.setSentDate(new Date());
fromAddress = new InternetAddress(user, fromName, "UTF-8");
msg.setFrom(fromAddress);
toAddress[0] = new InternetAddress(toEmail);
msg.setRecipients(Message.RecipientType.TO, toAddress);
if (null != cc) {
Address[] ccAdresses = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++) {
ccAdresses[i] = new InternetAddress(cc[i]);
}
// 将抄送者信息设置到邮件信息中
msg.setRecipients(Message.RecipientType.CC, ccAdresses);
}
// 标题
msg.setSubject(subject, "UTF-8");
// 文本体
msg.setContent(body, "text/html;charset = utf-8");
msg.setText(body, "UTF-8");
msg.saveChanges();
// 发送
Transport.send(msg, msg.getAllRecipients());
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置:
mail.port=465
mail.stmp=smtp.mxhichina.com
mail.from=xxxx@xxx.com
mail.password=xxxxxxxx
mail.factory=javax.net.ssl.SSLSocketFactory
阿里企业邮箱:
25端口已被封禁,所以使用465端口,
需加入设置
props.setProperty("mail.smtp.socketFactory.class",SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback","false");
端口:
25(smtp),简单邮件传输协议
465(smtps)smtp-over-SSL 协议