2012-02-24 47 views
1

嗨,我有寫代碼,使用Java郵件Java郵件javax.mail.AuthenticationFailedException

這裏發送從我的服務器的郵件是我的代碼

Properties pros = new Properties(); 
pros.put("mail.smtp.host", "my Ip Adress"); 
pros.put("mail.smtp.auth", "true"); 
pros.put("mail.smtp.port", "25"); 
Session mailSession = Session.getDefaultInstance(pros, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication("emailId","password"); 
       } 
      }); 
Message message = new MimeMessage(mailSession); 
message.setFrom(new InternetAddress("sendingAddress")); 
message.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse("recieverAddress")); 
message.setSubject("Testing Subject"); 
message.setText("Dear Mail Crawler," + 
        "\n\n No spam to my email, please!"); 
Transport.send(message); 
return "SUCCESS"; 

所有我在我的代碼中提供的細節正確,我的意思是屬性設置,我已經檢查過它。但不幸的是它表明,如身份驗證一些錯誤信息失敗

的錯誤信息是這樣的

javax.mail.AuthenticationFailedException 

任何人對此有什麼想法?

我正在爲我的項目使用struts2框架。

+0

看到類似的問題類似的線程http://stackoverflow.com/ questions/2047942/how-to-resovle-javax-mail-authenticationfailedexception-issue – 2012-02-24 09:19:09

回答

0

使用此代碼..!您正在使用得到驗證的傳統方法,從郵件服務器

import java.util.*; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.mail.Message; 
public void Mail(String to[], String subject, String content_message) 
     throws MessagingException { 

    boolean debug = false; 

    try { 
     String host = "smtp.gmail.com"; 
     String from = "[email protected]"; 
     String pass = "yourpwd"; 
     Properties props = System.getProperties(); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.user", from); 
     props.put("mail.smtp.password", pass); 
     props.put("mail.smtp.port", "587"); 
     props.put("mail.smtp.auth", "true"); 
     Session session = Session.getDefaultInstance(props, null); 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(from)); 
     InternetAddress[] toAddress = new InternetAddress[to.length]; 
     for (int i = 0; i < to.length; i++) { 
      toAddress[i] = new InternetAddress(to[i]); 
     } 
     System.out.println(Message.RecipientType.TO); 
     for (int i = 0; i < toAddress.length; i++) { 
      message.addRecipient(Message.RecipientType.TO, toAddress[i]); 
     } 
     message.setSubject(subject); 

     message.setContent(content_message, "text/html; charset=\"UTF-8\""); 
     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, from, pass); 
     transport.sendMessage(message, message.getAllRecipients()); 
     transport.close(); 
    } 

    catch (Exception e) { 
     System.out.println("Unable to connect"); 
    } 
} 

呼叫,當您需要發送郵件的構造..

+0

請注意,沒有「mail.smtp.password」屬性,因此沒有理由設置它。還要注意,你真的應該使用Session.getInstance()而不是Session.getDefaultInstance()。 – 2012-02-24 20:36:07

+0

這段代碼適合我。任何如何,我會得到一些確認後,根據您的建議更正我的代碼。感謝您的參與。 – 2012-02-26 11:31:51