Java Mail-附加多个pdf文件并发送邮件

时间:2019-05-06 20:44:26

标签: playframework javamail outputstream pdf-writer

我想附加多个pdf文件(根据收据数量,例如,如果有2张收据,则应创建两个附件),然后使用javamail发送邮件。 我可以附加一个文件并发送电子邮件,但是当我要进行多个附件时,pdf附件中没有内容。

我尝试过的:-

用于创建pdf文件:-

//rentReceiptId Will store receipt id of available receipt
String rentReceiptId[] = selectedCards.split(",");
int count = rentReceiptId.length;
byte[] bytes ={};
String filename[] = new String[count];
String byteArray[] = new String[count];


//For each receipt id create a pdf file
for(int i=0 ; i< rentReceiptId.length; i++) {

   ByteArrayOutputStream outputStream = null;
   try {
     outputStream = new ByteArrayOutputStream();
     outputStream.close();
     Document document = new Document();
     PdfWriter.getInstance(document, outputStream);
     document.open();
     document.addTitle("Rent Receipt PDF");
     document.addSubject("Testing email PDF");
     HTMLWorker htmlWorker=new HTMLWorker(document);
     String str = "<html><head></head><body>"+ "Contents that to be sent" +"</body></html>";
     htmlWorker.parse(new StringReader(str));
     document.addKeywords("iText, email");
     document.addAuthor("Test");
     document.addCreator("Test");
     document.close();
     bytes = outputStream.toByteArray();
     filename[i] = "RentReceipt-"+i+".pdf";
     String temp = new String(bytes);
     byteArray[i] = temp;

 }catch(Exception ex) {
                        ex.printStackTrace();
                    }


CompletableFuture.runAsync( () -> {
                    Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );

                        EmailUtils.sendEmailMultipleAttachment(mailerClient,recipientId, "Email Rent Receipt  Attachment","Please follow below attachment",recipientName,filename,byteArray);

                    }, Executors.newSingleThreadExecutor()).exceptionally(exc ->  {
                exc.printStackTrace();
                return null;});

}

EmailUtils.java:-

public class EmailUtils {
public static void sendEmailMultipleAttachment(MailerClient mailerClient,String recEmail, String subject, String content, String userName,String[] filename, String[] multiObject){
        Email email = new Email()
                    .setSubject(subject)
                    .setFrom("Test <info@Test.com>")
                    .addTo(userName + " <" + recEmail + ">")
                    .setBodyHtml("html Content");

for(int i=0; i< filename.length ; i++) {
                     byte[] fileContents = 
  multiObject[i].getBytes(Charset.forName("UTF-8"));


  //Not Working
  email.addAttachment(fileName[i], fileContents ,"application/pdf");

}

 try{

                mailerClient.send(email);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }


正在附加多个文件,但这些文件中没有内容。 当我尝试使用“单个文件”时,它可以正常工作,哪里出了问题?

1 个答案:

答案 0 :(得分:0)

更改

 String temp = new String(bytes);
 byteArray[i] = temp;
 bytes = outputStream.toByteArray();

 bytes = outputStream.toByteArray();
 String temp = new String(bytes);
 byteArray[i] = temp;

顺便说一句,将字符串数组命名为“ byteArray”不是一个好选择。 :-)

只传递一个字节数组也更好, 然后使用JavaMail的ByteArrayDataSource添加附件,但是 我不知道您的Email类是否可以解决这个问题。

将字节数组转换为字符串,然后再转换回字节数组 可能会损坏内容。