/**
* 发送支持html标签的邮件
*
* @param smtp_host 邮件服务地址
* @param smtp_port 邮件服务端口
* @param smtp_addr 发送方邮箱地址
* @param smtp_user 发送方邮箱用户名
* @param smtp_pwd 发送方邮箱密码
* @param sendAddr 接收方邮箱地址
* @param mailTitle 主题
* @param mailContents 正文
* @param files 附件(没有为null)
* @param cc 是否以给自己发送给目的方抄送的方式发送(解决邮箱判定垃圾拒绝发送554错误)
* @return: boolean
* @date: 2022/1/24 14:20
*/
public boolean sendHtmlEmail(String smtp_host, int smtp_port, String smtp_addr, String smtp_user, String smtp_pwd, String sendAddr, String mailTitle, String mailContents, List<File> files, boolean cc,String sslpro) {
Transport transport = null;
try {
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
//需要验证
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.ssl.protocols", sslpro);
//协议
props.setProperty("mail.smtp.host", smtp_host);
//smtp主机名
props.put("mail.smtp.port", smtp_port);
//端口
//预防transport.send()无限超时
props.put("mail.smtp.connectiontimeout", 60000);
props.put("mail.smtp.timeout", 120000);
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
//配置ssl 参数
props.setProperty("mail.smtp.socketFactory.port", Integer.toString(465));
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.user", smtp_user);
//发送方邮箱用户名
props.put("mail.smtp.password", smtp_pwd);
//发送方邮箱密码
//会话
String userName = smtp_user;
String password = smtp_pwd;
PopupAuthenticator popA = new PopupAuthenticator();
//填写用户名及密码
popA.performCheck(userName, password);
Session sendMailSession = Session.getInstance(props, popA);
//消息
Message message = new MimeMessage(sendMailSession);
//发送方邮箱地址
message.setFrom(new InternetAddress(smtp_addr));
if (cc) {
//以给自己发送给别人抄送的方式,避免邮箱判定垃圾邮件给拒绝发送
//接收方邮件地址(设为自己)
message.setRecipient(Message.RecipientType.TO, new InternetAddress(smtp_addr));
//抄送
message.setRecipient(Message.RecipientType.CC, new InternetAddress(sendAddr));
} else {
//接收方邮件地址
message.setRecipient(Message.RecipientType.TO, new InternetAddress(sendAddr));
}
//主题
message.setSubject(mailTitle);
//时间
message.setSentDate(new Date());
//正文支持html标签
MimeMultipart multipart = new MimeMultipart("related");
BodyPart bodyPart = new MimeBodyPart();
//正文
bodyPart.setContent(mailContents, "text/html;charset=UTF-8");
multipart.addBodyPart(bodyPart);
//附件
if (files != null && files.size() > 0) {
for (File file : files) {
MimeBodyPart fileBody = new MimeBodyPart();
DataSource source = new FileDataSource(file);
fileBody.setDataHandler(new DataHandler(source));
fileBody.setFileName(MimeUtility.encodeText(source.getName(), "utf-8", null));
multipart.addBodyPart(fileBody);
}
}
message.setContent(multipart);
transport = sendMailSession.getTransport("smtp");
transport.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (transport != null) {
transport.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class PopupAuthenticator extends Authenticator {
String username = null;
String password = null;
public PopupAuthenticator() {
}
public PasswordAuthentication performCheck(String user, String pass) {
username = user;
password = pass;
return getPasswordAuthentication();
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
发送邮箱 工具类
于 2022-07-15 11:11:25 首次发布