将多个文件附加到电子邮件中

时间:2012-02-06 21:58:07

标签: google-app-engine javax.mail

我一直在努力解决这个问题,但我找不到问题的答案。 方案如下:  一个使用Play框架的Web应用程序在Google应用程序引擎上发布。试图将2 pdf文件附加到电子邮件并发送。有一个文件,它工作得很好。有两个我得到错误。 这是代码:     包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("myAddress@gmail.com", "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,我也没有错误消息,但我猜我的是DataSource对象存在问题。我感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您应该使用FileDataSource代替DataSource类型,而不是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);