com.sun.mail.smtp.SMTPSendFailedException: 501 5.1.7 Syntax error in sender address
原因:SMTP 協議(RFC 5321/5322)要求郵箱地址純 ASCII,中文顯示名需 **MIME 編碼**(`=?UTF-8?B?base64?=`)。
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
String from = "測試 <test@163.com>";
// 正確構造
InternetAddress addr = new InternetAddress("test@163.com", "測試", "UTF-8");
message.setFrom(addr);
或手動編碼:
String encoded = MimeUtility.encodeText("測試") + " <test@163.com>";
message.setFrom(new InternetAddress(encoded));
前台顯示完整「測試 <test@163.com>」,後台解析提取純郵箱 `test@163.com` 發送,查詢時再拼接中文名。雖可行但不符合規範,易導致反垃圾郵件系統拒收。
統一使用 `InternetAddress` 構造器指定 charset,確保 RFC 合規,提高送達率。