首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何解决javax.mail.AuthenticationFailedException问题?

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

Stack Overflow用户
提问于 2010-01-12 17:41:29
回答 6查看 128.8K关注 0票数 17

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

sendMailServlet代码:

代码语言:javascript
复制
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上的输出:

代码语言:javascript
复制
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
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2010-01-12 19:39:26

您需要实现自定义Authenticator

代码语言:javascript
复制
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中使用它

代码语言:javascript
复制
Session session = Session.getInstance(props, new GMailAuthenticator(username, password));

也可以查看JavaMail FAQ

票数 20
EN

Stack Overflow用户

发布于 2015-08-10 20:07:28

此错误来自google安全...这可以通过启用不太安全的解决方案来解决。

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

票数 20
EN

Stack Overflow用户

发布于 2011-06-14 03:04:41

我在下面的代码行中遗漏了这个验证器对象参数

代码语言:javascript
复制
Session session = Session.getInstance(props, new GMailAuthenticator(username, password));

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2047942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档