如何解决 javax.mail.AuthenticationFailedException 问题?

Posted

技术标签:

【中文标题】如何解决 javax.mail.AuthenticationFailedException 问题?【英文标题】:How to resolve javax.mail.AuthenticationFailedException issue? 【发布时间】:2011-01-04 02:41:52 【问题描述】:

我正在做一个sendMail ServletJavaMail。我的输出上有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

【问题讨论】:

异常到底什么时候发生?我在你的日志中看不到它 【参考方案1】:

你需要实现一个自定义的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 FAQ

【讨论】:

@n002213f : 当我从 jndi 调用 (jboss) 的 mailservice.xml 获取会话时,如何使用这个 GMAILAuthenticator。 @Ashwin - 看看类似问题的答案***.com/questions/3475971/… @n002213f :你提供了一个绝妙的解决方案,现在我正在实施这个只是将 smtp gmail 服务器替换为我自己的 smtp 服务器名称,但得到 java.lang.NoClassDefFoundError: javax/mail/Authenticator 错误(因为我在 jsp/servlet 的帮助下实现它)...任何输入.. 很好的答案,但我更喜欢第二个(乌玛的答案)。我的身份验证器有效,但谷歌的标准(幸运的是)太严格了。不过,+1。 我面临着完全相同的问题,尝试了这个和第二个答案。两者都不适合我,我正在使用 tomcat 作为 Web 容器。【参考方案2】:

此错误来自谷歌安全... 这可以通过启用不太安全来解决。

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

【讨论】:

哦,tq :) 。我为此奋斗了很久。这不应该发生在任何 One 身上。所以,我把它做得干净利落 Phil C 它对我有用,但如何在不让不太安全的应用程序使用 IMAP 的情况下使其工作?【参考方案3】:

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

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

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

【讨论】:

【参考方案4】:

当您尝试从新设备或应用程序登录您的 Google 帐户时,您必须解锁 CAPTCHA。 要解锁验证码,请转到https://www.google.com/accounts/DisplayUnlockCaptcha 然后

还要确保allow less secure apps on

【讨论】:

【参考方案5】:

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

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

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

【讨论】:

【参考方案6】:

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

【讨论】:

以上是关于如何解决 javax.mail.AuthenticationFailedException 问题?的主要内容,如果未能解决你的问题,请参考以下文章

如何解决包冲突问题

如何解决包冲突问题

如何解决ajax跨域问题

MySQL 的 10048问题,如何解决?

如何解决smartgit的冲突问题

如何解决https传输图片的问题