更新
This commit is contained in:
parent
886e9f356d
commit
2b06f3d50f
@ -1,15 +1,10 @@
|
||||
package com.dc.common.utils;
|
||||
|
||||
|
||||
import javax.mail.Message;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @Description: 邮箱工具类
|
||||
@ -44,73 +39,79 @@ public class MailUtils {
|
||||
* @param emailPassword 发件人邮箱授权码
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean sendEmail(Set<String> emails, String title, String sendEmail, String content, String emailProtocol, String emailSMTPHost, String emailPort, String emailAccount, String emailPassword) {
|
||||
public static boolean sendEmail(Set<String> emails, String title, String sendEmail, String content,
|
||||
String emailProtocol, String emailSMTPHost, String emailPort,
|
||||
String emailAccount, String emailPassword) {
|
||||
// 未传收件人邮箱地址则直接返回
|
||||
if (emails == null || emails.isEmpty()) return false;
|
||||
|
||||
try {
|
||||
// 1. 创建参数配置, 用于连接邮件服务器的参数配置
|
||||
// 1. 创建参数配置, 用于连接邮件服务器
|
||||
Properties props = new Properties();
|
||||
props.setProperty("mail.transport.protocol", emailProtocol); // 使用的协议(JavaMail规范要求)
|
||||
props.setProperty("mail.smtp.host", emailSMTPHost); // 指定smtp服务器地址
|
||||
props.setProperty("mail.smtp.port", emailPort); // 指定smtp端口号
|
||||
// 使用smtp身份验证
|
||||
props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
|
||||
props.put("mail.smtp.ssl.enable", "true"); // 开启SSL
|
||||
props.put("mail.smtp.ssl.protocols", "TLSv1.2"); // 指定SSL版本
|
||||
props.setProperty("mail.transport.protocol", "smtp"); // 使用SMTP协议
|
||||
props.setProperty("mail.smtp.host", emailSMTPHost); // SMTP服务器
|
||||
props.setProperty("mail.smtp.port", emailPort); // SMTP端口号
|
||||
props.setProperty("mail.smtp.auth", "true"); // 启用SMTP身份验证
|
||||
props.setProperty("mail.smtp.ssl.enable", "true"); // 启用SSL
|
||||
props.put("mail.smtp.starttls.enable", "true"); // 启用STARTTLS(适用于587端口)
|
||||
props.put("mail.smtp.ssl.protocols", "TLSv1.2"); // 指定TLS版本
|
||||
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
||||
// 由于Properties默认不限制请求时间,可能会导致线程阻塞,所以指定请求时长
|
||||
props.setProperty("mail.smtp.connectiontimeout", "10000");// 与邮件服务器建立连接的时间限制
|
||||
props.setProperty("mail.smtp.timeout", "10000");// 邮件smtp读取的时间限制
|
||||
props.setProperty("mail.smtp.writetimeout", "10000");// 邮件内容上传的时间限制
|
||||
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
|
||||
Session session = Session.getDefaultInstance(props);
|
||||
session.setDebug(false); // 设置为debug模式, 可以查看详细的发送log
|
||||
|
||||
// 设置请求超时时间
|
||||
props.setProperty("mail.smtp.connectiontimeout", "10000");
|
||||
props.setProperty("mail.smtp.timeout", "10000");
|
||||
props.setProperty("mail.smtp.writetimeout", "10000");
|
||||
|
||||
// 2. 创建会话
|
||||
Session session = Session.getInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(emailAccount, emailPassword);
|
||||
}
|
||||
});
|
||||
|
||||
session.setDebug(false); // 关闭调试日志
|
||||
|
||||
// 3. 创建邮件
|
||||
MimeMessage message = new MimeMessage(session);
|
||||
// 4. From: 发件人(昵称有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改昵称)
|
||||
message.setFrom(new InternetAddress(emailAccount, sendEmail, "UTF-8"));
|
||||
// 5. To: 收件人(可以增加多个收件人、抄送、密送)
|
||||
// MimeMessage.RecipientType.TO: 发送 MimeMessage.RecipientType.CC:抄送 MimeMessage.RecipientType.BCC:密送
|
||||
int size = emails.size();
|
||||
// 单个目标邮箱还是多个
|
||||
if (size == 1) {
|
||||
String email = emails.iterator().next();
|
||||
message.setRecipient(Message.RecipientType.TO, new InternetAddress(email, email, "UTF-8"));
|
||||
|
||||
// 4. 添加收件人
|
||||
if (emails.size() == 1) {
|
||||
message.setRecipient(Message.RecipientType.TO, new InternetAddress(emails.iterator().next()));
|
||||
} else {
|
||||
InternetAddress[] addresses = new InternetAddress[emails.size()];
|
||||
int i = 0;
|
||||
for (String email : emails) {
|
||||
addresses[i++] = new InternetAddress(email, email, "UTF-8");
|
||||
}
|
||||
message.setRecipients(MimeMessage.RecipientType.TO, addresses);
|
||||
InternetAddress[] addresses = emails.stream()
|
||||
.map(email -> {
|
||||
try {
|
||||
return new InternetAddress(email);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.toArray(InternetAddress[]::new);
|
||||
message.setRecipients(Message.RecipientType.TO, addresses);
|
||||
}
|
||||
// 6. Subject: 邮件主题(标题有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改标题)
|
||||
|
||||
// 5. 邮件标题 & 内容
|
||||
message.setSubject(title, "UTF-8");
|
||||
// 7. Content: 邮件正文(可以使用html标签)(内容有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改发送内容)
|
||||
message.setContent(content, "text/html;charset=UTF-8");
|
||||
// 8. 设置发件时间
|
||||
message.setSentDate(new Date());
|
||||
// 9. 保存设置
|
||||
message.saveChanges();
|
||||
// 10. 根据 Session 获取邮件传输对象
|
||||
Transport transport = session.getTransport();
|
||||
transport.connect(emailAccount, emailPassword);
|
||||
// 11. 发送邮件, 发到所有的收件地址, message.getAllRecipients()获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
|
||||
transport.sendMessage(message, message.getAllRecipients());
|
||||
// 12. 关闭传输连接
|
||||
transport.close();
|
||||
|
||||
// 6. 发送邮件
|
||||
Transport.send(message);
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
System.out.println("报错"+e);
|
||||
System.out.println("发送邮件失败:" + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Set<String> set = new HashSet<>();
|
||||
set.add("18766413289@163.com");
|
||||
// System.out.println(MailUtils.sendEmail(set, "测试发送邮件的接口!",
|
||||
// "您好!这是我发送的一封测试发送接口的邮件,看完请删除记录。","stmp", "smtp.qq.com", "465", "18766413289@163.com", "tbpxqvywlanfchhe"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user