2012-08-23 159 views
0

我使用下面的代碼爲什麼Transport.sendMessage在其中一個地址無效時失敗?

public void send() throws MessagingException 
    { 
     // create some properties and get the Session 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", this.getSMTPServer()); 

     if(this.getDebugMode()) 
      props.put("mail.debug", "true"); 
     else 
      props.put("mail.debug", "false"); 
     //Comment by Sandip for FIRSTMEDIA-578 
     //props.put("mail.smtp.auth", "true"); 

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

     MimeMessage mail = new MimeMessage(session); 

     //Set Mailer string in Header.. 
     mail.setHeader("X-Mailer", this.getMailer()); 

     //Set TO Recipients, toList would be of comma separated if multiple addresses are there 
     if(this.getTo() != null) 
     { 
      StringTokenizer st = new StringTokenizer(this.getTo(),","); 
      InternetAddress[] recList = new InternetAddress[st.countTokens()]; 

      for (int r = 0; st.hasMoreTokens(); r++) 
       recList[r] = new InternetAddress(st.nextToken().trim());   

      if(recList.length != 0) 
      { 
       mail.setRecipients(Message.RecipientType.TO, recList); 
      } 

     }   


     //Set CC Recipients, bccList would be of comma separated if multiple addresses are there 
     if(this.getCc() != null) 
     { 
      StringTokenizer st1 = new StringTokenizer(this.getCc(),","); 
      InternetAddress[] copyList = new InternetAddress[st1.countTokens()]; 
      for (int c = 0; st1.hasMoreTokens(); c++) 
       copyList[c] = new InternetAddress(st1.nextToken().trim());   

      if(copyList.length != 0) 
       mail.setRecipients(Message.RecipientType.CC, copyList); 
     }   


     //Set BCC Recipients, bccList would be of comma separated if multiple addresses are there 
     if(this.getBcc() != null) 
     { 
      StringTokenizer st2 = new StringTokenizer(this.getBcc(),","); 
      InternetAddress[] bcopyList = new InternetAddress[st2.countTokens()]; 
      for (int c = 0; st2.hasMoreTokens(); c++) 
       bcopyList[c] = new InternetAddress(st2.nextToken().trim());   

      if(bcopyList.length != 0) 
       mail.setRecipients(Message.RecipientType.BCC, bcopyList); 
     } 


     // Create a mime message 
     mail.setFrom(new InternetAddress(this.getFrom())); 
     mail.setSubject(subject); 


     //create mulitple parts to added 
     Multipart mp = new MimeMultipart(); 

     MimeBodyPart mbp1 = new MimeBodyPart(); 

     //messageMIME can be "text/plain" or "text/html" or anything related to mime 
     mbp1.setContent(this.getMsgText(),this.getContentType()); 
     mp.addBodyPart(mbp1); 

     //Adding attachments to mail 
     if(this.attachmentList != null) 
     { 
      for(int i=0; i < this.attachmentList.size() ; i++) 
      { 
       MimeBodyPart mbp = new MimeBodyPart(); 
       FileDataSource fds = new FileDataSource((File)this.attachmentList.elementAt(i)); 
       mbp.setDataHandler(new DataHandler(fds)); 
       mbp.setFileName(fds.getName()); 
       mp.addBodyPart(mbp); 
      } 
     } 

     mail.setContent(mp); 
     mail.saveChanges(); 

     mail.setSentDate(new Date()); 

     // Send the message 
     Transport trans = session.getTransport("smtp"); 
     trans.connect(this.getSMTPServer(), this.getSMTPUsername(), this.getSMTPPassword()); 
     trans.sendMessage(mail, mail.getAllRecipients()); 
     trans.close(); 

    } 

有了這個當郵件發送失敗給一個收件人出收件人的所有收件人的郵件發送失敗的名單發送郵件。

是否有任何更改要求從列表中發送郵件給所有正確的郵件收件人?

+0

是收件人地址無效?你能提供一個失敗案例嗎? – Vikdor

+0

並非所有列表,但列表中的一個列表無效 – chetan

回答

0

如果檢查Doc,發送消息稱:。 「將消息發送到指定的地址列表中選擇合適TransportEvent指示交貨狀態傳送到這個交通運輸註冊的任何TransportListener另外,如果任意地址, ,是無效的,一個SendFailedException拋出是否仍發送成功地以任何有效的地址,而不是信息取決於運輸實行「

你可以嘗試:

  • 在ONW發送一個你代碼或
  • 您可以捕獲例外SendFailedException並檢查 getValidUnsentAddresses以發送到 未發送的有效地址。

我真的喜歡第二個:)

檢查Sent Failed Exception

相關問題