javax.mail:mail:1.4.7 或 Spring Boot spring-boot-starter-mailAuthenticator,提供用戶名/密碼認證public class MailSenderInfo {
private String mailServerHost = "smtp.163.com";
private String mailServerPort = "25"; // SSL 用 465/587
private String fromAddress;
private String toAddress;
private String userName;
private String password;
private boolean validate = true;
private String subject;
private String content;
private String[] attachFileNames;
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");
// SSL 配置
// p.put("mail.smtp.ssl.enable", "true");
// p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
return p;
}
// getter/setter 略
}
public class SimpleMailSender {
// 發送純文本
public boolean sendTextMail(MailSenderInfo info) {
Session session = createSession(info);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(info.getFromAddress()));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(info.getToAddress()));
msg.setSubject(info.getSubject());
msg.setSentDate(new Date());
msg.setText(info.getContent()); // 純文本
Transport.send(msg);
return true;
} catch (MessagingException e) { e.printStackTrace(); return false; }
}
// 發送 HTML
public boolean sendHtmlMail(MailSenderInfo info) {
Session session = createSession(info);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(info.getFromAddress()));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(info.getToAddress()));
msg.setSubject(info.getSubject());
msg.setSentDate(new Date());
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(info.getContent(), "text/html; charset=utf-8");
multipart.addBodyPart(htmlPart);
msg.setContent(multipart);
Transport.send(msg);
return true;
} catch (MessagingException e) { e.printStackTrace(); return false; }
}
// 發送帶附件
public boolean sendAttachMail(MailSenderInfo info) {
Session session = createSession(info);
try {
MimeMessage msg = new MimeMessage(session);
// ... 設置發件人、收件人、主題、時間同前
MimeMultipart multipart = new MimeMultipart();
// 文本/HTML 部分
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(info.getContent(), "text/html; charset=utf-8");
multipart.addBodyPart(textPart);
// 附件部分
if (info.getAttachFileNames() != null) {
for (String filePath : info.getAttachFileNames()) {
MimeBodyPart attachPart = new MimeBodyPart();
DataSource source = new FileDataSource(filePath);
attachPart.setDataHandler(new DataHandler(source));
// 解決中文附件名亂碼
attachPart.setFileName(MimeUtility.encodeText(new File(filePath).getName()));
multipart.addBodyPart(attachPart);
}
}
msg.setContent(multipart);
Transport.send(msg);
return true;
} catch (Exception e) { e.printStackTrace(); return false; }
}
private Session createSession(MailSenderInfo info) {
Authenticator auth = null;
if (info.isValidate()) {
auth = new MyAuthenticator(info.getUserName(), info.getPassword());
}
return Session.getInstance(info.getProperties(), auth);
}
}
public class MyAuthenticator extends Authenticator {
private String username, password;
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
public class MailTest {
public static void main(String[] args) {
MailSenderInfo info = new MailSenderInfo();
info.setMailServerHost("smtp.qq.com");
info.setMailServerPort("465"); // SSL
info.setValidate(true);
info.setUserName("your@qq.com");
info.setPassword("授權碼"); // 非 QQ 密碼
info.setFromAddress("your@qq.com");
info.setToAddress("target@163.com");
info.setSubject("JavaMail 測試 - " + System.currentTimeMillis());
info.setContent("HTML 郵件測試
包含中文內容
");
info.setAttachFileNames(new String[]{"D:/test.pdf", "D:/image.png"});
SimpleMailSender sender = new SimpleMailSender();
sender.sendAttachMail(info); // 一鍵發送帶附件 HTML 郵件
}
}
MimeUtility.encodeText() 編碼mail.smtp.sendpartial、分片發送或上傳雲存儲發鏈接