2011-11-02 113 views
0

我使用java郵件從服務器發送郵件。我使用下面的代碼,但我得到這個錯誤..顯示授權失敗的Java郵件

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException 
    at mail.mailSSL.main(mailSSL.java:53) 
Caused by: javax.mail.AuthenticationFailedException 
    at javax.mail.Service.connect(Service.java:306) 
    at javax.mail.Service.connect(Service.java:156) 
    at javax.mail.Service.connect(Service.java:105) 
    at javax.mail.Transport.send0(Transport.java:168) 
    at javax.mail.Transport.send(Transport.java:98) 
    at mail.mailSSL.main(mailSSL.java:48) 

我的代碼:

public class mailSSL { 
    public static void main(String args[]){ 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", 
       "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 

     Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication("[email protected]","[email protected]"); 
       } 
      }); 

     try { 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse("[email protected]")); 
      message.setSubject("SFTS MAIL TESING"); 
      message.setText("Dear Mail Crawler," + 
        "\n\n No spam to my email, please!"); 

      Transport.send(message); 

      System.out.println("Done"); 

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

我怎樣才能解決這個問題?

+0

你可能想要在上面的帖子中用「*」來編輯和替換你的密碼。您是否嘗試過簡單而簡單的mail.smtp.user或mail.smtp.password? – aishwarya

回答

0

我會相信有一個消息異常沒有被捕獲。

1

你的班級完全符合我的憑據。你有Gmail帳戶嗎?當我通過代碼嘗試你的代碼時,我得到以下例外:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 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 ib2sm1330072vdb.1