2016-08-22 119 views
0

我試圖使用javax.mail包從Outlook 2010發送電子郵件,但此代碼沒有工作,我「M確保郵件和密碼是否正確線程「main」中的異常java.lang.RuntimeException:javax.mail.AuthenticationFailedException:535 5.7.3身份驗證失敗

import java.io.IOException; 
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; 

public class SendMail { 

public static void main(String[]args) throws IOException { 

    final String username = "[email protected]"; 
    final String password = "something"; 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", "outlook.office365.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("Test"); 
     message.setText("HI"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 
}} 

它給了我下面的錯誤:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful 

at SendMail.main(SendMail.java:45) 
Caused by: javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful 

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:932) 
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:843) 
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:748) 
at javax.mail.Service.connect(Service.java:388) 
at javax.mail.Service.connect(Service.java:246) 
at javax.mail.Service.connect(Service.java:195) 
at javax.mail.Transport.send0(Transport.java:254) 
at javax.mail.Transport.send(Transport.java:124) 
at SendMail.main(SendMail.java:40) 
Java Result: 1 

這是我第一次與郵件API工作,所以請原諒我任何明顯的錯誤。

+0

爲什麼使用outlook的smtp服務器登錄到gmail帳戶? – byxor

+0

這個人發佈了一個看起來很健全的代碼和一個完整的回溯,並且有一個有用的標題。沒有理由降票。 –

+0

這是一個我在網上找到的示例代碼,我認爲如果我把電子郵件和密碼,它會將我視爲前景..正如我所說我是新的,並且我不熟悉Outlook世界。所以任何幫助將不勝感激。 – John

回答

0

1.要申請一個outlook帳戶,如[email protected],並更改示例代碼中的用戶名和密碼變量。

2.在示例代碼中將'mail.smtp.host'修改爲'smtp.live.com'。