2010-01-12 189 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