17

我正在做一个sendMail Servletwith JavaMail。我有javax.mail.AuthenticationFailedException我的输出。谁能帮帮我?谢谢。

sendMailServlet 代码:

try {
        String host = "smtp.gmail.com";
        String from = "my@gmail.com";
        String pass = "pass";
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");

        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        Address fromAddress = new InternetAddress(from);
        Address toAddress = new InternetAddress("test1@gmail.com");

        message.setFrom(fromAddress);
        message.setRecipient(Message.RecipientType.TO, toAddress);

        message.setSubject("Testing JavaMail");
        message.setText("Welcome to JavaMail");
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        message.saveChanges();
        Transport.send(message);
        transport.close();

    }catch(Exception ex){

        out.println("<html><head></head><body>");
        out.println("ERROR: " + ex);
        out.println("</body></html>");
    }

GlassFish 2.1 上的输出:

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP 36sm10907668yxh.13
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
EHLO platform-4cfaca
250-mx.google.com at your service, [203.126.159.130]
250-SIZE 35651584
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35651584"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO platform-4cfaca
250-mx.google.com at your service, [203.126.159.130]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35651584"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
aWpveWNlbGVvbmdAZ21haWwuY29t
334 UGFzc3dvcmQ6
MTIzNDU2Nzhf
235 2.7.0 Accepted
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
4

6 回答 6

20

您需要实现一个自定义Authenticator

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


class GMailAuthenticator extends Authenticator {
     String user;
     String pw;
     public GMailAuthenticator (String username, String password)
     {
        super();
        this.user = username;
        this.pw = password;
     }
    public PasswordAuthentication getPasswordAuthentication()
    {
       return new PasswordAuthentication(user, pw);
    }
}

现在在Session

Session session = Session.getInstance(props, new GMailAuthenticator(username, password));

另请查看JavaMail 常见问题解答

于 2010-01-12T11:39:25.670 回答
20

此错误来自 google security... 这可以通过启用 Less Secure 来解决。

转到此链接:“ https://www.google.com/settings/security/lesssecureapps ”并“打开”,然后您的应用程序肯定会运行。

于 2015-08-10T12:07:28.093 回答
2

我在下面的行中缺少了这个验证器对象参数

Session session = Session.getInstance(props, new GMailAuthenticator(username, password));

这条线解决了我的问题,现在我可以通过我的 Java 应用程序发送邮件了。其余的代码很简单,就像上面一样。

于 2011-06-13T19:04:41.430 回答
1

当您尝试从新设备或应用程序登录您的 Google 帐户时,您必须解锁 CAPTCHA。要解锁 CAPTCHA,请访问https://www.google.com/accounts/DisplayUnlockCaptcha ,然后在此处输入图像描述

并确保允许不太安全的应用程序 在此处输入图像描述

于 2019-09-02T05:25:42.790 回答
0

问题是,您正在创建一个transport对象并使用它的连接方法来验证自己。但是随后您使用一种static方法来发送忽略对象完成的身份验证的消息。

因此,您应该使用sendMessage(message, message.getAllRecipients())对象上的方法或使用其他人建议的身份验证器来通过会话获得授权。

这是Java Mail FAQ,您需要阅读。

于 2012-11-26T06:22:10.220 回答
0

只是想和大家分享一下:
我在更改Digital Ocean机器(IP地址)后碰巧得到了这个错误。显然,Gmail 将其识别为黑客攻击。遵循他们的指示并批准新的 IP 地址后,代码将恢复并运行。

于 2014-09-04T14:02:14.157 回答