【问题标题】:javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587
【发布时间】:2013-08-21 04:07:21
【问题描述】:

我有一个发送电子邮件的 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

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

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);

    }
}

【问题讨论】:

    标签: java jakarta-mail


    【解决方案1】:

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

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

    【讨论】:

    • 我已经尝试过使用端口 465 的代码,但它仍然失败并出现同样的异常。关于如何解决这个问题的任何建议?
    【解决方案2】:

    添加这些属性对我有帮助:

    debug=true
    spring.mail.host=smtp.gmail.com
    spring.mail.port=587
    spring.mail.username=user@gmail.com
    spring.mail.password=somepwd
    
    spring.mail.properties.mail.debug=true
    spring.mail.properties.mail.transport.protocol=smtp
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.ssl.trust=*
    spring.mail.properties.mail.smtp.starttls.enable=true
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-01
      • 2014-03-08
      • 2017-10-07
      • 2020-04-23
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多