2014-08-31 306 views
0

我試圖從我的應用程序發送電子郵件,但一次又一次地獲得身份驗證失敗錯誤。我已經嘗試了給定的解決方案,但他們不工作,因此我想把我的代碼。請看下面的代碼,並告訴我是否缺少重要的東西。通過gmail發送電子郵件時身份驗證失敗

package managimg.stud.data; 

import java.util.Date; 
import java.util.Properties; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.Multipart; 
import javax.mail.Session; 
import sendmail.Sendmail; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

public class SendMail { 
    public static void main(String...a){ 
     SendMail s = new SendMail(); 
    } 

    public SendMail(){ 

     Authenticator authenticate = new Authenticator(){ 
     public PasswordAuthentication getPasswrodAuthentication(){ 
      return new PasswordAuthentication("abcd","xyz"); 
     } 
     }; 

     String to = "[email protected]"; 
     String from = "[email protected]"; 
     String host = "smtp.gmail.com"; 

     Properties properties = System.getProperties(); 
     properties.setProperty("mail.smtp.host", "smtp.gmail.com"); 
     properties.setProperty("mail.smtp.port", "587"); 
     properties.setProperty("mail.smtp.starttls.enable", "true") ; 
     properties.setProperty("mail.smtp.auth", "true") ; 
     properties.setProperty("mail.smtp.user", "abcd"); 
     properties.setProperty("mail.smtp.password", "xyz"); 
     properties.setProperty("mail.smtp.debug", "true"); 

     Session session = Session.getDefaultInstance(properties,authenticate); 

     try{ 

     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(from)); 
     message.addRecipient(Message.RecipientType.TO, 
           new InternetAddress(to)); 
     message.setSubject("This is the Subject Line!"); 
     message.setText("This is actual message"); 

     Transport.send(message); 
     System.out.println("Sent message successfully...."); 
     }catch (MessagingException mex) { 
     mex.printStackTrace(); 
     } 

    } 

} 

有人能告訴我這裏有什麼問題嗎?我錯過了什麼?我一次又一次得到下面的錯誤。我已經嘗試了很多解決方案,但它不起作用。任何人都可以幫助我嗎?

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 managimg.stud.data.SendMail.<init>(SendMail.java:139) 
at managimg.stud.data.SendMail.main(SendMail.java:31) 
+1

Google昨天通過gmail smtp服務器更改了發送帶有遠程發件人地址的電子郵件的權限評估。您可以在互聯網上找到有關信息以及備選方案和解決方法。 – arkascha 2014-08-31 14:44:53

+0

但我能ping通主機。現在關於權限可以請你提供任何我可以找到替代品的鏈接。 – 2014-08-31 14:59:41

+0

我已經嘗試了幾乎所有在堆棧溢出中給出的解決方案來連接gmail或任何其他郵件服務器,但沒有解決方案正在工作。每當我得到相同的錯誤。任何人都可以提供一些可行和正確的解決方案我也嘗試過簡單地複製和粘貼來自各個網站的互聯網上提供的代碼,但它們不能很好地工作。 – 2014-08-31 15:09:02

回答

2

這就是我如何解決我的錯誤。請找到下面的代碼,剛纔我發送了一封測試郵件。

package gmail.email; 

import java.util.Properties; 
import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.*; 
import javax.mail.PasswordAuthentication; 


public class GmailEmail { 

    final String userName ="[email protected]"; 
    final String password="tqw12"; 

    public static void main(String[] args) { 
     new GmailEmail(); 
    } 

    public GmailEmail(){ 
     Properties properties = new Properties(); 
     properties.put("mail.smtp.host", "smtp.gmail.com"); 
     properties.put("mail.smtp.port", "587"); 
     properties.put("mail.smtp.starttls.enable", "true") ; 
     properties.put("mail.smtp.auth", "true") ; 

     Session session = Session.getInstance(properties,new 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("my First Email"); 
      message.setContent("<h:body>You wrote first email</body>","text/html;  charset=utf-8"); 
      Transport.send(message); 
     }catch(MessagingException 
       messageException){ 
      throw new RuntimeException(messageException); 
     } 

    } 

} 

我只用兩個jar:

  1. 的mail.jar
  2. 的JavaEE-API-6.0.jar

,我的JDK版本是JDK6.0。

相關問題