[英]Could not connect to SMTP host: smtp.gmail.com, port: 587 on windows server 2016
I am trying to send an email from a gmail account from windows server 2016 from a Java application and I get the error : "Could not connect to SMTP host: smtp.gmail.com, port: 587."我正在尝试从 Java 应用程序从 Windows Server 2016 的 gmail 帐户发送电子邮件,但出现错误:“无法连接到 SMTP 主机:smtp.gmail.com,端口:587。”
When trying on my local machine, which runs windows 10, the code works perfectly.在运行 Windows 10 的本地计算机上尝试时,代码运行良好。 I have used mail enable and port 587 is enabled and also used as an alternate port to listen to for SMTP.
我使用了邮件启用并启用了端口 587,并且还用作侦听 SMTP 的备用端口。 Also, I configured an inbound rule for this port on windows server.
另外,我在 Windows 服务器上为此端口配置了入站规则。 I still have the same error.
我仍然有同样的错误。 Does anyone know what I can do ?
有谁知道我能做什么?
I will also add the code.我也会添加代码。
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);
}
}
}
Is that gmail account configured to allow 'unsecure apps'?该 Gmail 帐户是否配置为允许“不安全的应用程序”? You may need to setup an app token to allow such direct connections to gmail.
您可能需要设置一个应用令牌以允许与 Gmail 的这种直接连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.