2014-10-27 15 views
4

我想用JavaMailAPIJavaMail的異常javax.mail.AuthenticationFailedException特定應用程序534-5.7.9密碼所需

我已經做了一些代碼來發送郵件,但它不工作引發異常: -

留言發送Failedjavax.mail.AuthenticationFailedException:534-5.7.9需要應用程序特定的密碼。

package com.appreciationcard.service; 

import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import com.appreciationcard.dao.DAOFactory; 
import com.appreciationcard.forms.ComposeForm; 

public class MailServiceImpl implements MailService { 

public boolean mailsent(ComposeForm composeForm) { 
    String to = composeForm.getTo(); 
    String from = composeForm.getFrom(); 
    String cc = composeForm.getCc(); 
    String bcc = composeForm.getBcc(); 
    String subject = composeForm.getSubject(); 
    String messages = composeForm.getMessage(); 
    Properties props = new Properties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", true); 
    props.put("mail.smtp.port", "587"); 
    props.put("mail.transport.protocol", "smtp"); 
    props.put("mail.debug", "true"); 
    System.out.println("Properties" + props); 
    Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(
          "[email protected]", "xxxx"); 
       } 
      }); 
    try { 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(
       to)); 
     message.setSubject(subject); 
     message.setText(messages); 
     Transport.send(message); 
    } catch (MessagingException mex) { 
     System.out.println("Message Sending Failed" + mex); 
     mex.printStackTrace(); 
    } 

} 

}

我服務器控制檯上獲得異常

消息發送Failedjavax.mail.AuthenticationFailedException:需要534-5.7.9應用程序專用密碼。

瞭解更多534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 o5sm11464195pdr.50 - gsmtp

將在任何一個好心幫我解決這個問題。

+0

要清楚,您是否按照鏈接中的說明生成了應用專用密碼?即使使用應用程序特定的密碼,您是否也會收到錯誤? – ZoogieZork 2014-10-27 18:21:04

+0

您是否閱讀過該頁面? – SLaks 2014-10-27 18:21:26

+0

在某些系統上,鏈接到上面列出的有用URL會被清除。這就是我所在的地方 - 所以這在這方面仍然是一個有用的問題。 – davidjmcclelland 2015-01-31 20:15:28

回答

7

您已啓用Two phase authentication作爲您的Google帳戶,因此應用程序將無法使用實際密碼登錄到您的Google帳戶。 Google希望您爲每個使用的應用程序生成一個特定於應用程序的密碼(併爲其命名),然後使用該密碼從您的應用程序登錄到您的Google帳戶。這使您在啓用兩步驗證時不會將密碼提供給第三方應用程序。

另一種方式是讓您的應用程序支持重定向到Google頁面,以使用用戶名和密碼以及Google身份驗證器應用生成的代碼進行身份驗證。

link明確解釋了該怎麼做。

+0

非常感謝您提供寶貴的信息。我的問題解決了郵件發送成功。感謝Mr.Saket的快速回復。謝謝 – 2014-10-27 18:35:42

0

錯誤消息和知識庫文章似乎很正確:您需要爲Google Mail服務器使用應用程序密碼。您可以在谷歌的賬戶頁面上創建這些丟棄密碼。

0

您可能試圖通過啓用雙因素身份驗證的gmail帳戶發送郵件。通過Google身份驗證器應用程序發送給您的移動設備的6位身份驗證令牌需要繼續。或者,您可以通過Google的基於Web的ui生成應用專用密碼,並在通過您的Java代碼訪問該郵件帳戶時使用該密碼。您的問題中的link you included會引導您完成整個過程。

相關問題