Could not connect to SMTP host: smtp.163.com, port: 25;阿里雲 ECS
阿新 • • 發佈:2019-02-04
ECS基於安全考慮,目前已禁用25埠。
如果您的傳送程式部署在阿里雲ECS上,建議您不勾選SSL時,使用80埠,勾選SSL時,使用465埠。
測試埠 telnet smtp.163.com 25
測試網路 ping smtp.163.com
import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class SimpleAliDMSendMail { private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com"; private static final int ALIDM_SMTP_PORT = 25; public static void main(String[] args) throws MessagingException { // 配置傳送郵件的環境屬性 final Properties props = new Properties(); // 表示SMTP傳送郵件,需要進行身份驗證 props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", ALIDM_SMTP_HOST); props.put("mail.smtp.port", ALIDM_SMTP_PORT); // 如果使用ssl,則去掉使用25埠的配置,進行如下配置, // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // props.put("mail.smtp.socketFactory.port", "465"); // props.put("mail.smtp.port", "465"); // 發件人的賬號 props.put("mail.user", "***"); // 訪問SMTP服務時需要提供的密碼 props.put("mail.password", "***"); // 構建授權資訊,用於進行SMTP進行身份驗證 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 使用者名稱、密碼 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用環境屬性和授權資訊,建立郵件會話 Session mailSession = Session.getInstance(props, authenticator); // 建立郵件訊息 MimeMessage message = new MimeMessage(mailSession); // 設定發件人 InternetAddress form = new InternetAddress( props.getProperty("mail.user")); message.setFrom(form); // 設定收件人 InternetAddress to = new InternetAddress("***"); message.setRecipient(MimeMessage.RecipientType.TO, to); // 設定郵件標題 message.setSubject("測試郵件"); // 設定郵件的內容體 message.setContent("測試的HTML郵件", "text/html;charset=UTF-8"); // 傳送郵件 Transport.send(message); } }