繁体   English   中英

无法连接到 SMTP 主机:smtp.gmail.com,端口:Windows Server 2016 上的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM