pom依赖:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
配置邮箱类
public class ExMailUtil {
// 这是企业邮箱的 如果是其他邮箱 自行更换
public static String MAIL_TRANSPORT_PROTOCOL = "smtp"; //邮箱协议
public static String MAIL_SMTP_HOST = "mail.xxxxx.com"; //发件服务器地址
public static String MAIL_SMTP_PORT = "45"; // 端口
public static String MAIL_SMTP_AUTH = "true"; //使用smtp身份验证
/**
* 邮箱配置
*/
public static Properties setTencentExEmail (){
Properties prop = new Properties();
//协议
prop.setProperty("mail.transport.protocol", MAIL_TRANSPORT_PROTOCOL);
//服务器
prop.setProperty("mail.smtp.host", MAIL_SMTP_HOST);
//端口
prop.setProperty("mail.smtp.port", MAIL_SMTP_PORT);
//使用smtp身份验证
prop.setProperty("mail.smtp.auth", MAIL_SMTP_AUTH);
prop.put("mail.smtp.starttls.enable", true);
//开启安全协议 使用SSL,企业邮箱必需!
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
return prop;
}
}
使用邮箱发消息
public void snedEmail(String messageTitle, String messageContent, String email) {
Session session = Session.getDefaultInstance(ExMailUtil.setTencentExEmail(), new Authenticator() {
//返回用户和密码的对象
@Override
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication(EMAIL_NAME, EMAIL_PASSWORD);
return pa;
}
});
MimeMessage mimeMessage = new MimeMessage(session);
Transport transport = null;
try {
mimeMessage.setFrom(new InternetAddress(EMAIL_NAME,EMAIL_NAME));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//设置主题
mimeMessage.setSubject(messageTitle);
mimeMessage.setSentDate(new Date());
//设置内容
mimeMessage.setText(messageContent);
//保存邮件
mimeMessage.saveChanges();
transport = session.getTransport("smtp");
//发送
transport.connect(EMAIL_NAME, EMAIL_PASSWORD);
//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
} catch (MessagingException e) {
log.error(e.getMessage());
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage());
}finally {
if(transport != null && transport.isConnected()) {
try {
transport.close();
} catch (MessagingException e) {
log.error(e.getMessage());
}
}
}
}
如果邮箱有通配符模板的话,可以替换通配符
通配符格式[[xxxxx]]
/**
* 解析[[data]] 内的内容 并用数据填充
*
* @param message 消息
* @param paramsMap map参数数据
* @return 解析之后的数据
*/
public static String parse(String message, Map<String, Object> paramsMap) {
if (isBlank(message)) return message;
//获取[[数据]] 中的数据内容
Pattern p = Pattern.compile("(?!=\\[)[^\\[\\]]+(?=\\])");
Matcher matcher = p.matcher(message);
while (matcher.find()) {
String group = matcher.group();
Object paramObj = paramsMap.get(group);
String result = paramObj != null ? paramObj.toString() : "";
message = message.replace("[[" + group + "]]", result);
}
return message;
}
注:如果发邮件是使用多线程异步发送的,请注意方法是否使用了事务,@Transactional修饰的方法可能事务还没有成功执行,数据库没有成功插入的情况下异步查询了表,这个时候有可能是查询不到数据的,可以将查询sql写到事务完成之后,也可以将需要替换的数值以map形式或其他实体类形式组装好,以参数的形式传递下去,直至替换并发送邮件