javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18312477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:23:40  来源:igfitidea点击:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587

javajavamail

提问by user407269

I have a java application which sends emails. But when i run the code i get the following exception.i have also check the authentication settings and with 485 port no but still error remains.

我有一个发送电子邮件的 Java 应用程序。但是当我运行代码时,我得到以下异常。我还检查了身份验证设置,并且 485 端口没有,但仍然存在错误。

DEBUG: setDebug: JavaMail version 1.4.4
DEBUG: getProvider() returning      
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems,   
Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 
587;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at SimpleMailWithAttachment.<init>(SimpleMailWithAttachment.java:46)
at SimpleMailWithAttachment.main(SimpleMailWithAttachment.java:64)
    Caused by: java.net.ConnectException: Connection refused: connect

Here is the code i am using to try to send mail

这是我用来尝试发送邮件的代码

public SimpleMailWithAttachment() {
    Properties props = System.getProperties();
    props.setProperty("mail.smtp.port", "587");
    props.setProperty("mail.smtp.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.starttls.enable", "true");
    props.setProperty("mail.smtp.auth", "true");

    Authenticator auth = new MyAuthenticator();
    Session smtpSession = Session.getInstance(props, auth);
    smtpSession.setDebug(true);

    MimeMessage message = new MimeMessage(smtpSession);
    try {
        message.setFrom(new InternetAddress(email));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        final String encoding = "UTF-8";

        message.setSubject(subject, encoding);
        message.setText(text, encoding);
        Transport.send(message);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

static class MyAuthenticator extends Authenticator {

    public PasswordAuthentication getPasswordAuthentication() {
        String username = "******@gmail.com";
        String password = "*****";
        return new PasswordAuthentication(username, password);

    }
}

回答by Matti Lyra

I don't think you can use GMail without authentication. You need to use port 465 not 587.

我不认为您可以在没有身份验证的情况下使用 GMail。您需要使用端口 465 而不是 587。

See http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm

请参阅http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm