2012-02-06 106 views
0

我一直在苦苦掙扎了一段時間,找不到解決問題的答案。 該場景如下: 在Google應用引擎中使用了一個使用play framework的Web應用程序。試圖將2個pdf文件附加到電子郵件併發送。有了一個文件,它完美地工作。有兩個我得到錯誤。 以下是代碼: package app;將多個文件附加到電子郵件中

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.util.Properties; 

import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.MimeType; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 
import javax.mail.util.ByteArrayDataSource; 


public class Mailer { 

    public static void sendEmail(String to, String subject, String body, byte[]  pdf1, byte[] pdf2) 
    throws AddressException, MessagingException, UnsupportedEncodingException{ 
     Properties props = new Properties(); 
     Session session = Session.getDefaultInstance(props, null); 
     Message msg = new MimeMessage(session); 
     msg.setFrom(new InternetAddress("[email protected]", "John Smith")); 
     msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, to)); 
     msg.setSubject(subject); 
     msg.setText(body); 

     Multipart mp = new MimeMultipart(); 

     MimeBodyPart htmlPart = new MimeBodyPart(); 
     htmlPart.setContent(body, "text/html"); 
     mp.addBodyPart(htmlPart); 

     //Attaching first pdf 
     MimeBodyPart attachment = new MimeBodyPart(); 
     attachment.setFileName("pdf1.pdf"); 
     DataSource src = new ByteArrayDataSource(pdf1, "application/pdf"); 
     attachment.setDataHandler(new DataHandler(src)); 
     mp.addBodyPart(attachment); 


     //Attaching second pdf 
     attachment = new MimeBodyPart(); 
     attachment.setFileName("pdf2.pdf"); 
     src = new ByteArrayDataSource(badgePDF, "application/pdf"); 
     attachment.setDataHandler(new DataHandler(src)); 
     mp.addBodyPart(attachment); 


     msg.setContent(mp); 

     Transport.send(msg); 
    } 


} 

不幸的是我,即使我打印捕獲異常的stackTrack沒有錯誤消息,但我我的猜測是,有與數據源對象的問題。我很感激任何幫助。

回答

0

對於您的DataSource類型,您應該使用FileDataSource而不是使用ByteArrayDataSource。嘗試以下方法:通過應用程序生成

Multipart mp = new MimeMultipart(); 
MimeBodyPart htmlPart = new MimeBodyPart(); 
htmlPart.setContent(body, "text/html"); 
mp.addBodyPart(htmlPart);  

File[] attachments = new File[2]; 
atts[1] = new File("pdf1.pdf"); 
atts[2] = new File("pdf2.pdf"); 
for(int i = 0; i < attachments.length; i++) { 
    messageBodyPart = new MimeBodyPart(); 
    FileDataSource fileDataSource =new FileDataSource(attachments[i]); 
    messageBodyPart.setDataHandler(new DataHandler(fileDataSource)); 
    messageBodyPart.setFileName(attachments[i].getName()); 
    mp.addBodyPart(messageBodyPart); 

} 

msg.setContent(mp); 
Transport.send(msg); 
+0

PDF和,因爲我不能保存在谷歌應用程序引擎的文件不會被保存在服務器上的文件中,這就是爲什麼數據是一個字節數組。 – 2012-02-06 22:47:22

+0

您是否嘗試過使用[MultiPartEmail](http://commons.apache.org/email/apidocs/org/apache/commons/mail/MultiPartEmail.html) – 2012-02-06 22:59:06

+0

據我所知我無法使用org.apache.commons。 – 2012-02-06 23:57:05