2015-09-05 60 views
0
import java.util.Properties; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.mail.Authenticator; 
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 MailSender { 

    private Session session; 
    private final String username; 
    private final String password; 

    public MailSender(String username, String password) { 
     this.username = username; 
     this.password = password; 
     init(username, password); 
    } 

    public final void init(String username, String password) { 
     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.ssl.trust", "smtp.gmail.com"); 
     props.put("mail.smtp.port", "587"); 

     session = Session.getInstance(props, new Authenticator() { 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(MailSender.this.username, MailSender.this.password); 
      } 
     }); 

    } 

    public boolean send(String recipient, String subject, String body) { 
     boolean status = false; 
     try { 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(username)); 
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); 
      message.setSubject(subject); 
      message.setText(body); 
      Transport.send(message); 
      status = true; 

     } catch (MessagingException ex) { 
      Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return status; 
    } 

    public static void main(String[] args) { 
     MailSender mailer = new MailSender("[email protected]", "******"); 
     //Userprojects u=new Userprojects(); 
     boolean status = mailer.send("[email protected]", "Testing Subject", "Testing message"); 
     System.out.println(status); 
    } 
} 

例外:Java郵件API不起作用?

我已經使用了正確的登錄電子郵件ID和密碼編譯&使用JavamailAPI和我runabble批處理文件中的命令執行這個程序給出如下

Authenitication未能例外:534.5。 7.14 https://account.google.com/continousSignIn

+1

,然後 「按指定下面給出異常」 。這是我們必須爲自己發現的那些巧妙的面試問題之一嗎?那是最後一個時期嗎?一個微縮的圖形圖像實際上是以微文本的形式存在的。或者你忘了包含它? – paxdiablo

+0

發佈錯誤的 – JavaLearner

+0

驗證失敗例外:534.5.7.14 https://account.google.com/continousSignIn –

回答

3

代碼運行得很好這裏。

我想你已經提供了不正確的用戶名和/或密碼。如果您啓用了雙向認證,請確保您提供的是正確的Application Specific Password

無關您的問題 - 在init方法中使用的參數是從未使用過。

所以,你可以:

  • init法,刪除了參數即,init();

  • 通行證MailSender實例init方法,即init(this);

即,不指定

public final void init() { 
    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.ssl.trust", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "587"); 

    session = Session.getInstance(props, new Authenticator() { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(MailSender.this.username, MailSender.this.password); 
     } 
    }); 
} 

public final void init(MailSender mailSender) { 
    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.ssl.trust", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "587"); 

    session = Session.getInstance(props, new Authenticator() { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(mailSender.username, mailSender.password); 
     } 
    }); 
} 
+0

工作正常謝謝你。 :) –

+0

不客氣。請接受這個答案,這將標記爲已解決的帖子,並且使用谷歌發現此問題的人可以更確定答案是否正確。 :) –