承接前兩篇(會話創建、郵件構建),本文聚焦 Transport 類發送流程,給出純文本、HTML、帶附件、帶內嵌圖片四種完整可運行示例,並說明 SMTP 認證、SSL/TLS、QQ/163/企業郵箱常見配置坑點。
Session 獲取協議實現:Transport transport = session.getTransport("smtp")。transport.connect(smtpHost, username, authCode) —— **注意:密碼欄填「授權碼」而非登錄密碼**。transport.sendMessage(msg, msg.getAllRecipients()); transport.close()。Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "465"); // SSL 端口
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true"); // 啟用 SSL
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getDefaultInstance(props);
session.setDebug(true); // 打印完整 SMTP 交互日志
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("sender@qq.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("receiver@163.com"));
msg.setSubject("JavaMail 測試 - 純文本");
msg.setText("你好,這是純文本郵件。", "UTF-8");
msg.setSentDate(new Date());
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect("sender@qq.com", "授權碼"); // QQ郵箱需開啟SMTP獲取授權碼
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
MimeMessage msg = new MimeMessage(session);
// ... 設置發件人、收件人、主題同前 ...
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<h1 style='color:#1890ff'>HTML 郵件</h1><p>支持<b>富文本</b>排版</p>", "text/html;charset=UTF-8");
MimeMultipart mp = new MimeMultipart("alternative");
mp.addBodyPart(htmlPart);
msg.setContent(mp);
msg.saveChanges();
// ... 發送流程同前 ...
MimeMessage msg = new MimeMessage(session);
// ... 基礎頭信息 ...
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText("請查收附件。", "UTF-8");
MimeBodyPart attachPart = new MimeBodyPart();
DataSource fds = new FileDataSource("/path/to/report.pdf");
attachPart.setDataHandler(new DataHandler(fds));
attachPart.setFileName(MimeUtility.encodeText("報告.pdf")); // 中文文件名編碼
MimeMultipart mp = new MimeMultipart("mixed");
mp.addBodyPart(textPart);
mp.addBodyPart(attachPart);
msg.setContent(mp);
msg.saveChanges();
MimeMessage msg = new MimeMessage(session);
// ...
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<img src='cid:logo' /><p>內嵌圖片測試</p>", "text/html;charset=UTF-8");
MimeBodyPart imgPart = new MimeBodyPart();
DataSource ids = new FileDataSource("/path/to/logo.png");
imgPart.setDataHandler(new DataHandler(ids));
imgPart.setHeader("Content-ID", "<logo>"); // 與 cid:logo 對應
MimeMultipart related = new MimeMultipart("related");
related.addBodyPart(htmlPart);
related.addBodyPart(imgPart);
MimeMultipart mixed = new MimeMultipart("mixed");
mixed.addBodyPart(new MimeBodyPart()); // 可再加附件
// 將 related 作為一個整體加入 mixed
MimeBodyPart relatedWrapper = new MimeBodyPart();
relatedWrapper.setContent(related);
mixed.addBodyPart(relatedWrapper);
msg.setContent(mixed);
msg.saveChanges();
| 郵箱 | SMTP 主機 | SSL 端口 | 非 SSL 端口 | 備註 |
|---|---|---|---|---|
| QQ郵箱 | smtp.qq.com | 465 | 587 (STARTTLS) | 需開啟「服務-賬號-POP3/SMTP」獲取授權碼 |
| 163郵箱 | smtp.163.com | 465/994 | 25 | 需開啟「設置-POP3/SMTP/IMAP」獲取授權碼 |
| Gmail | smtp.gmail.com | 465 | 587 (STARTTLS) | 需開啟「兩步驟驗證」生成應用專用密碼 |
| Outlook | smtp.office365.com | 587 (STARTTLS) | — | 企業郵箱通用 |
mail.smtp.starttls.enable=true。MimeUtility.encodeText() 編碼,防止亂碼。mail.smtp.timeout、mail.smtp.connectiontimeout,考慮分片發送。session.setDebug(true) 能輸出完整 SMTP 指令交互,排查連接、認證、發送失敗的利器。