2010-06-03 109 views
8

當我嘗試使用JavaMail API發送郵件時出現此錯誤。我確信用戶名和密碼是100%正確的。我連接的Gmail帳戶是一個較舊的帳戶,因爲他們說需要時間才能使用新帳戶。帶Gmail的JavaMail:535-5.7.1不接受用戶名和密碼

 
DEBUG SMTP RCVD: 535-5.7.1 Username and Password not accepted. Learn more at 

535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 x35sm3011668 
wfh.6 

javax.mail.SendFailedException: Sending failed; 
    nested exception is: 
     javax.mail.AuthenticationFailedException 
     at javax.mail.Transport.send0(Transport.java:218) 
     at javax.mail.Transport.send(Transport.java:80) 
     at Main.(Main.java:41) 
     at Main.main(Main.java:51) 

,這是我的代碼:

import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 

public class Main 
{ 
    String d_email = "[email protected]", 
      d_password = "pass", 
      d_host = "smtp.gmail.com", 
      d_port = "465", 
      m_to = "[email protected]", 
      m_subject = "Testing", 
      m_text = "testing email."; 

    public Main() 
    { 
     Properties props = new Properties(); 
     props.put("mail.smtp.user", d_email); 
     props.put("mail.smtp.host", d_host); 
     props.put("mail.smtp.port", d_port); 
     props.put("mail.smtp.starttls.enable","true"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.debug", "true"); 
     props.put("mail.smtp.socketFactory.port", d_port); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 

     SecurityManager security = System.getSecurityManager(); 

     try 
     { 
      Authenticator auth = new SMTPAuthenticator(); 
      Session session = Session.getInstance(props, auth); 
      session.setDebug(true); 
      MimeMessage msg = new MimeMessage(session); 
      msg.setText(m_text); 
      msg.setSubject(m_subject); 
      msg.setFrom(new InternetAddress(d_email)); 
      msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to)); 
      Transport.send(msg); 
     } 
     catch (Exception mex) 
     { 
      mex.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) 
    { 
     Main blah = new Main(); 
    } 

    private class SMTPAuthenticator extends javax.mail.Authenticator 
    { 
     public PasswordAuthentication getPasswordAuthentication() 
     { 
      return new PasswordAuthentication(d_email, d_password); 
     } 
    } 
} 
+4

@bobby:你的密碼的base64編碼值在原來的職位是可見的。我在一小時前刪除了它,但它在編輯歷史記錄中仍然可見。我強烈建議您在惡意黑客破壞您的Gmail帳戶之前更改您的Gmail密碼。 – BalusC 2010-06-03 12:06:34

回答

5

給定的代碼段工作正常,在我的Gmail帳戶,所以這個問題就出在別處。你關注了the link given in the error message嗎?它包含以下提示:

  • 確保您輸入完整的電子郵件地址(例如[email protected]
  • 重新輸入密碼,以確保它是正確的。請記住,密碼區分大小寫。
  • 確保您的郵件客戶端未設置爲經常檢查新郵件。如果您的郵件客戶端每10分鐘檢查一次以上的新郵件,您的客戶端可能會重複請求您的用戶名和密碼。

特別是最後一點很重要。谷歌對此非常嚴格。如果您嘗試以編程方式在一分鐘內將Gmail連接超過10次,則可能已被阻止。有一點耐心,過一段時間後,它會暢通無阻。

如果您希望發送郵件更自由,我建議您尋找專用郵件主機或設置自己的郵件服務器,例如Apache James或Microsoft Exchange。在您之前的問題中,我已經有answered this in detail

+0

hhey謝謝...它工作.. !!你能告訴我如何使用jsp發送郵件.. !!即時通訊試圖在網站上實現'忘記密碼'是否有任何想法和建議從你來實現這個..! – simplyblue 2010-06-03 12:03:24

+0

只需創建一個servlet類來完成任務並在JSP中創建一個HTML表單,該表單將提交給此Servlet正在偵聽的URL。在servlet方法中,您可以使用通常的方式編寫和調用真正的Java代碼。在這裏瞭解更多關於JSP/Servlet的信息:http://courses.coreservlets.com/Course-Materials/csajsp2.html – BalusC 2010-06-03 12:15:04

2

我遇到了完全相同的問題,對我來說,原因是我已打開我的gmail帳戶2-step verification

在生成新的應用程序專用密碼並在我的Java應用程序中使用該密碼後,此「535 5.7.1」問題消失了。

您可以在此official google guide之後生成新的應用程序專用密碼。

+0

謝謝! – Davidea 2017-09-08 13:47:14

0

我有同樣的錯誤消息,這是我已經解決了這個問題,

創建一個應用程序密碼:這裏是我們如何能夠生成一個應用密碼,

1. Visit your App passwords page. You may be asked to sign in to your Google Account. 

2. At the bottom, click Select app and choose the app you’re using. 
Click Select device and choose the device you’re using. 

3. Select Generate. 

4. Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device. 

5. Select Done. 

我工作了春季啓動應用程序,我得到的應用程序密碼說,sadsadaffferere的電子郵件地址,​​。所以,我需要配置的應用屬性,如下面

# the email settings 
# ------------------ 
spring.mail.host=smtp.gmail.com 
[email protected] 
spring.mail.password=sadsadaffferere 
spring.mail.properties.mail.smtp.auth=true 
spring.mail.properties.mail.smtp.socketFactory.port=465 
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory 
spring.mail.properties.mail.smtp.socketFactory.fallback=false 
[email protected] 

一切正常之後

相關問題