2010-01-12 190 views
16

我正在做sendMailServletJavaMail。我的输出上有javax.mail.AuthenticationFailedException。任何人都可以请帮我吗?谢谢。如何解决javax.mail.AuthenticationFailedException问题?

sendMailServlet代码:

try { 
     String host = "smtp.gmail.com"; 
     String from = "[email protected]"; 
     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("[email protected]"); 

     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 
+0

什么时候发生异常?我看不到它在你的日志 – 2010-01-12 09:44:05

回答

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 FAQ

+0

甜,这对我有用! – PapaFreud 2011-03-07 15:57:22

+0

@ n002213f:当我通过jndi调用(jboss)从mailservice.xml获取会话时,如何使用此GMAILAuthenticator。 – Ashwin 2012-06-26 10:41:56

+0

@Ashwin - 看看类似问题的答案http://stackoverflow.com/questions/3475971/configure-the-mail-service-xml-in-jboss-with-a-gmail-account – n002213f 2012-06-27 07:22:02

2

我是缺少在下面的线

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

该验证对象参数此行解决了我的问题,现在我可以通过我的Java应用程序发送邮件。 其余代码很简单,就像上面一样。

0

问题是,您正在创建一个transport对象并使用它的connect方法来验证自己。 但是,您可以使用static方法发送忽略对象所做验证的消息。

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

这里是Java Mail FAQ,你需要阅读。

0

只是想与大家分享:
我在更换Digital Ocean机器(IP地址)后碰巧遇到此错误。 Gmail显然认为它是一种黑客攻击。按照他们的指示,并批准新的IP地址后,代码返回并运行。

20

大家好这个错误来自谷歌安全... 这可以通过安全性较低来解决。

转到此链接:“https://www.google.com/settings/security/lesssecureapps”并使“开启”,那么你的应用程序运行为确定。

+0

简单而干净。 +5为伟大的链接 – 2015-12-11 02:29:39

+2

哦,tq :)。我为此付出了很多努力。这不应该发生在任何一个。所以,我把它变得清洁和明确菲尔C – UmaShankar 2015-12-14 09:51:56

+0

它适用于我,但如何使它工作,而不必让较不安全的应用程序使用IMAP? – gouessej 2016-01-22 10:42:55