简体   繁体   English

无法连接到 SMTP 主机:smtp.gmail.com,端口:Windows Server 2016 上的 587

[英]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.

相关问题 javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587 - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587 无法连接到SMTP主机:smtp.gmail.com,端口:465 - could not connect to smtp host:smtp.gmail.com, port:465 javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587; java.net.NoRouteToHostException:主机没有路由 - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; java.net.NoRouteToHostException: No route to host 无法连接到SMTP主机:smtp.gmail.com,port:587; 嵌套异常是:java.net.ConnectException:连接超时:连接 - Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection timed out: connect javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:587无法管理解决此问题 - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587 Can not manage to solve this problem smtp.gmail.com,端口:587 失败 - smtp.gmail.com, port: 587 fails ERROR ANDROID:无法连接到SMTP主机:smtp.gmail.com,port:465,响应:-1 - ERROR ANDROID: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 无法连接到SMTP主机:smtp.gmail.com,port:465,响应:-1 - Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:465; - javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587; 超时-1 - com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM