2015-12-16 309 views
0

我有下面的代碼。無法通過Gmail發送郵件smtp

public static void main(String[] args) 
     { 
      sendMessage("hello"); 
     } 

     private static void sendMessage(String s) { 

      Properties props = new Properties();   

      props.put("mail.smtp.auth", "true"); 
      props.put("mail.smtp.starttls.enable", "true"); 
      props.put("mail.smtp.host", "smtp.gmail.com"); 
      props.put("mail.smtp.port", "587");   

      Session session = Session.getInstance(props, new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password); 
       } 
      }); 

      try {    
       Message message = new MimeMessage(session); 
       message.setFrom(new InternetAddress("[email protected]")); 
       message.setRecipients(Message.RecipientType.TO, 
         InternetAddress.parse("[email protected]")); 
       message.setSubject("subject"); 
       message.setText(s); 
       Transport.send(message); 
      } catch (MessagingException e) { 
       throw new RuntimeException(e); 
      } 
     } 

密碼和用戶名有效。但例外是我得到的唯一的東西:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtN2 
534-5.7.14 -5TkN4GaweC6hf3rGNq9_jNkn3li8xHQJsvsrBtvsgYtTItKuGxzOh0JxrqqrtlGEKUfRj 
534-5.7.14 ObHLIRLXI877ZwP120TCNTgXfCCj-sxKvs4LMuwQTm-MlRVj43NtxpqZAWM7MtzxwCde0w 
534-5.7.14 -NrEnlwJVoqdYPvJjsbZyzN4wL-nETdGCAkC7wSOv_56NCEMh4riOM_M5AlR4Uf0FnsZB3 
534-5.7.14 PdfTBLfznxA9Q6yesAh5ib8uebGU> Please log in via your web browser and 
534-5.7.14 then try again. 
534-5.7.14 Learn more at 
534 5.7.14 https://support.google.com/mail/answer/78754 dz6sm720370lbb.17 - gsmtp 

    at ru.tenet.es09.runnable.SendMailRunnable.sendMessage(SendMailRunnable.java:62) 
    at ru.tenet.es09.runnable.SendMailRunnable.main(SendMailRunnable.java:32) 
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtN2 
534-5.7.14 -5TkN4GaweC6hf3rGNq9_jNkn3li8xHQJsvsrBtvsgYtTItKuGxzOh0JxrqqrtlGEKUfRj 
534-5.7.14 ObHLIRLXI877ZwP120TCNTgXfCCj-sxKvs4LMuwQTm-MlRVj43NtxpqZAWM7MtzxwCde0w 
534-5.7.14 -NrEnlwJVoqdYPvJjsbZyzN4wL-nETdGCAkC7wSOv_56NCEMh4riOM_M5AlR4Uf0FnsZB3 
534-5.7.14 PdfTBLfznxA9Q6yesAh5ib8uebGU> Please log in via your web browser and 
534-5.7.14 then try again. 
534-5.7.14 Learn more at 
534 5.7.14 https://support.google.com/mail/answer/78754 dz6sm720370lbb.17 - gsmtp 

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809) 
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669) 
    at javax.mail.Service.connect(Service.java:317) 
    at javax.mail.Service.connect(Service.java:176) 
    at javax.mail.Service.connect(Service.java:125) 
    at javax.mail.Transport.send0(Transport.java:194) 
    at javax.mail.Transport.send(Transport.java:124) 
    at ru.tenet.es09.runnable.SendMailRunnable.sendMessage(SendMailRunnable.java:60) 
    ... 1 more 

兩步驗證未啓用我的綽號。做什麼?

回答

2

您可以打開gmail中的「不太安全的應用程序」功能,以便您的代碼可以運行。你可以訪問你的谷歌console

除此之外,你可能想要更新你的代碼如何訪問現在的Gmail的gmail API here

相關問題