2013-04-29 104 views
0

最近我一直在自動每日郵件發件人加上我的項目兩個不同線程內的每週郵件。JavaMail - 任務結束,沒有錯誤,但沒有郵件發送

郵件服務器是微軟Exchange(不記得版本)

當只有每日郵報正在運行,我的郵件被送到就好了。 現在,我已經添加了另一個線程每週郵件,我有一些問題:

  • 線程只有一個能夠發送電子郵件(從來都)
  • 沒有線程能夠發送電子郵件

在我的日誌中我沒有錯誤的證據,它似乎與smtp服務器的連接不是問題,並且郵件已發送,但是當我檢查我的郵箱時,沒有郵件一切都到了。

我會後你的代碼我的電子郵件類

public class Email { 

boolean debug = false ; 
String smtpServer = null; 
int smtpPort = null; 
String smtpSender = null; 
String smtpUser = null; 
String smtpPassword = null; 


public Email(){ 
     smtpServer = Config.SMTP_SERVER; 
     smtpPort = Config.SMTP_PORT; 
     smtpSender = Config.SMTP_SENDER; 
     smtpUser = Config.SMTP_USER; 
     smtpPassword = Config.SMTP_PASSWORD; 
} 



public void postMailAttach(String recipients[], String subject, String message, String filename) throws MessagingException { 


Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 

     Session session = Session.getInstance(props, null); 


     //SET SERVER FOR MESSAGE 
     Message msg = new MimeMessage(session); 
     msg.setFrom(new InternetAddress(smtpSender)); 



     InternetAddress[] toAddress = new InternetAddress[recipients.length]; 

     for (int i = 0; i < recipients.length; i++){ 
     toAddress[i] = new InternetAddress(recipients[i]); 
     } 

     //SET RECIPIENTS FOR MESSAGE 
     msg.setRecipients(Message.RecipientType.TO, toAddress);  
     //SET SUBJECT 
     msg.setSubject(subject); 

     //SET BODY PART OF MESSAGE 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText(message); 

     Multipart multipart = new MimeMultipart(); 
     multipart.addBodyPart(messageBodyPart); 
     messageBodyPart = new MimeBodyPart(); 

     //GET FILES TO ATTACH 
     DataSource source = new FileDataSource(filename); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     //SET FILE NAME 
     messageBodyPart.setFileName(filename); 

     multipart.addBodyPart(messageBodyPart); 
     msg.setContent(multipart); 

     //SEND THE EMAIL 
     Transport transport = session.getTransport("smtp");  
     transport.connect(smtpServer,smtpPort,smtpUser,smtpPassword);  
     transport.sendMessage(msg,msg.getAllRecipients());  
     transport.close();   

} 

public void postMail (String recipients[], String subject, String message) throws MessagingException{ 

    //Set the host smtp address 
Properties props = new Properties(); 
props.put("mail.smtp.auth", "true"); 


// create some properties and get the default Session 
Session session = Session.getDefaultInstance(props, null); 
//session.setDebug(debug); 

// create a message 
Message msg = new MimeMessage(session); 

// set the from and to address 
InternetAddress addressFrom = new InternetAddress(smtpSender); 
msg.setFrom(addressFrom); 

InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
for (int i = 0; i < recipients.length; i++) { 
    addressTo[i] = new InternetAddress(recipients[i]); 
} 
msg.setRecipients(Message.RecipientType.TO, addressTo); 


// Optional : You can also set your custom headers in the Email if you Want 
//msg.addHeader("MyHeaderName", "myHeaderValue"); 

// Setting the Subject and Content Type 
msg.setSubject(subject); 
msg.setContent(message, "text/plain"); 
    Transport transport = session.getTransport("smtp");  
    transport.connect(smtpServer,smtpPort,smtpUser,smtpPassword);  
    transport.sendMessage(msg,msg.getAllRecipients());  
    transport.close();   


} 

感謝您的建議任何建議。

+0

我沒有看到線程在這裏,但你說,這個問題是在這個類的多線程使用。 – Pino 2013-04-29 11:17:39

+0

有2個獨立的線程。線程只是容器,用於檢查何時發送電子郵件,哪些消息,哪些主題和哪些收件人,僅此而已。發送作業全部通過Email類執行 – Daniel83 2013-04-29 11:19:47

+0

兩個線程是否共享'Email'類的相同對象,或者每個線程是否創建了其自己的'Email'類對象? – Apurv 2013-04-29 11:22:52

回答

0

initializating喜歡的用戶名的屬性,密碼等

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.port", "587"); 

Session session = Session.getInstance(props, 
    new javax.mail.Authenticator() { 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(username, password); 
    } 
    }); 

希望之後,這將有助於

+0

我會嘗試這個,讓你知道 – Daniel83 2013-04-29 11:22:40

+0

寫它而不是'Session session = Session.getInstance(props,null);'並刪除傳輸部分。 – smttsp 2013-04-29 11:24:00

+0

我已經添加了這部分,並刪除了transport.connect()部分,但現在我收到「未連接」的例外 – Daniel83 2013-04-29 11:32:56

相關問題