[英]javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587
[英]Could not connect to SMTP host: smtp.gmail.com, port: 587 on windows server 2016
我正在嘗試從 Java 應用程序從 Windows Server 2016 的 gmail 帳戶發送電子郵件,但出現錯誤:“無法連接到 SMTP 主機:smtp.gmail.com,端口:587。”
在運行 Windows 10 的本地計算機上嘗試時,代碼運行良好。 我使用了郵件啟用並啟用了端口 587,並且還用作偵聽 SMTP 的備用端口。 另外,我在 Windows 服務器上為此端口配置了入站規則。 我仍然有同樣的錯誤。 有誰知道我能做什么?
我也會添加代碼。
package email;
import java.sql.Timestamp;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GoogleMail {
public void sendEmail(String emailContent) {
final String username = "findyourbets2020@gmail.com";
final String password = "*********";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("findyourbets2020@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("testemail@gmail.com"));
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
message.setSubject("Application Failed at" + timestamp);
message.setText(emailContent);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
該 Gmail 帳戶是否配置為允許“不安全的應用程序”? 您可能需要設置一個應用令牌以允許與 Gmail 的這種直接連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.