JavaMail:无法为传输到Google Apps的域发送SMTP邮件

时间:2016-08-15 09:16:34

标签: java javamail google-apps

我最近将电子邮件移至Google Apps。现在,我可以通过登录gmail.com上的电子邮件帐户查看和发送邮件。

sales@example.com,现在我正在尝试发送SMTP电子邮件,这些电子邮件在较旧的托管上使用以前的SMTP设置正常工作。

这是我的代码

Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.debug", "true");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("sales@example.com", "my-new-password-on-gmail");
            }
        });

这是我看到的错误

EHLO 192.168.56.1
250-smtp.gmail.com at your service, [xxx.xx.xxx.xx an ip address]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtb
534-5.7.14 bABxM5_Dmk2f4asdasd86czsyvAdPqDTVjasdasdR2uN3N
534-5.7.14 bZAMByd76RlHwaGfasdasdoMS4pvXFhJOeoGhTwfRXWU2csadasd-zsuhCGiH7LUJsvrP_
534-5.7.14 DGJ_H79hQJlasdasdXNk0q_lRiiSGjDxsum4cZqQ6asdasdasdasdasdefI3-GjhEXGqCi5g
534-5.7.14 fZk_9lOFdPY9asduyernmdfhdfwm-k> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 n13oiuy5458013wmd.3 - gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
    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 com.vdif.web.commons.SmtpMailSender.send(SmtpMailSender.java:104)
    at com.vdif.web.commons.SmtpMailSender.send(SmtpMailSender.java:33)
    at com.vdif.web.commons.SmtpMailSender.main(SmtpMailSender.java:116)

1 个答案:

答案 0 :(得分:0)

港口号码465用于启用SSL(smtps)。其属性如下:

        properties.put("mail.smtps.host", "smtp.gmail.com");
        properties.put("mail.smtps.port", "465");
        properties.put("mail.smtps.auth", "true");
        properties.setProperty("mail.smtp.ssl.enable", "true");
        properties.setProperty("mail.transport.protocol", "smtps");

对于港口号。 587

    properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");
        properties.setProperty("mail.smtp.starttls.enable", TRUE);
        properties.setProperty("mail.smtp.starttls.required", TRUE);
        properties.setProperty("mail.smtp.ssl.enable", "false");
        properties.setProperty("mail.transport.protocol", "smtp");

有效。!