每次發郵件都要寫 Session、MimeMessage、Transport、Authenticator……為了專注業務,我們將核心邏輯封裝為三個類,打包成 com.util.mail,引用即用。
package com.util.mail;
import java.util.Properties;
public class MailSenderInfo {
// 服務器配置
private String mailServerHost;
private String mailServerPort = "25";
// 發件人/收件人
private String fromAddress;
private String toAddress;
// 認證
private String userName;
private String password;
private boolean validate = false;
// 郵件內容
private String subject;
private String content;
private String[] attachFileNames; // 附件路徑陣列
// 產生 JavaMail Session 所需 Properties
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.smtp.host", mailServerHost);
p.put("mail.smtp.port", mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
// ... 所有字段的 getter/setter 省略
}
package com.util.mail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SimpleMailSender {
/** 發送純文本郵件 */
public boolean sendTextMail(MailSenderInfo mailInfo) {
Session session = createSession(mailInfo);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(mailInfo.getFromAddress()));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailInfo.getToAddress()));
msg.setSubject(mailInfo.getSubject());
msg.setSentDate(new Date());
msg.setText(mailInfo.getContent()); // 純文本
Transport.send(msg);
return true;
} catch (MessagingException e) { e.printStackTrace(); return false; }
}
/** 發送 HTML 郵件 */
public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
Session session = createSession(mailInfo);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(mailInfo.getFromAddress()));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailInfo.getToAddress()));
msg.setSubject(mailInfo.getSubject());
msg.setSentDate(new Date());
// MimeMultipart 容器
Multipart mp = new MimeMultipart();
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mp.addBodyPart(htmlPart);
msg.setContent(mp);
Transport.send(msg);
return true;
} catch (MessagingException e) { e.printStackTrace(); return false; }
}
/** 統一建立 Session(支援認證) */
private static Session createSession(MailSenderInfo info) {
Authenticator auth = null;
if (info.isValidate()) {
auth = new MyAuthenticator(info.getUserName(), info.getPassword());
}
return Session.getDefaultInstance(info.getProperties(), auth);
}
}
package com.util.mail;
import javax.mail.*;
public class MyAuthenticator extends Authenticator {
private String userName, password;
public MyAuthenticator() {}
public MyAuthenticator(String user, String pass) {
this.userName = user; this.password = pass;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
public static void main(String[] args) {
MailSenderInfo info = new MailSenderInfo();
info.setMailServerHost("smtp.163.com");
info.setMailServerPort("25");
info.setValidate(true);
info.setUserName("yourname@163.com");
info.setPassword("yourpassword"); // 授權碼而非登入密碼
info.setFromAddress("yourname@163.com");
info.setToAddress("target@example.com");
info.setSubject("測試主題 - " + System.currentTimeMillis());
info.setContent("HTML 內容測試
當前時間:" + new Date() + "
");
SimpleMailSender sender = new SimpleMailSender();
sender.sendHtmlMail(info); // 或 sendTextMail(info)
System.out.println("發送完成");
}
com.util.mail),否則編譯/執行報錯。smtp.163.com 發信,fromAddress 必須是 @163.com,跨域會被拒收。mail.smtp.ssl.enable=true 或 mail.smtp.starttls.enable=true。sendHtmlMail 中遍歷 attachFileNames,建立 MimeBodyPart 並 attachFile 加入 Multipart。Session.getInstance(props, auth) 並緩存。CompletableFuture 或線程池,避免阻塞業務線程。這套封裝已在多個生產項目穩定運行 5 年+,核心原理不變,只有細節配置隨郵箱服務商策略微調。掌握「配置分離、認證封裝、內容多態」三原則,JavaMail 即變得簡單可控。