javax.mail附PDF-File坏了

时间:2015-01-29 11:53:49

标签: java pdf itext javamail

我正在开发一个程序,该程序应该通过javax将填充的PDF文件作为附件发送到Mail。为了测试我发送邮件到我自己的地址,发送itselft完全正常,但附加的PDF文件似乎被打破。

我的PDF阅读器告诉我,该文件可能被错误地解码了。经过一些研究后,我添加了行

 filePart.setHeader("Content-Transfer-Encoding", "base64");
 filePart.setHeader("Content-Type", "application/pdf");

到下面的代码,但它仍无效。

try
    {
        String filename = "C:/somerandompath/PDFTest.pdf";
        reader = new PdfReader(filename);
        tempFile = File.createTempFile("FORM" , ".pdf");
        fostream = new FileOutputStream(tempFile);

        stamper = new PdfStamper(reader , fostream);
        acro_fields = stamper.getAcroFields();

        Iterator entries = pdfOutputs.entrySet().iterator();
        while (entries.hasNext())
        {
            Entry thisEntry = (Entry)entries.next();
            acro_fields.setField((String)thisEntry.getKey() , (String)thisEntry.getValue());
        }


        /** Sending the Mail */
        try
        {
            Properties properties = new Properties();
            properties.put("mail.smtp.host" , "servername");
            javax.mail.Session sess = javax.mail.Session.getDefaultInstance(properties , null);
            DataSource fileDataSource = new FileDataSource(tempFile.getAbsolutePath());
            BodyPart filePart = new MimeBodyPart();
            MimeMultipart content = new MimeMultipart();
            MimeBodyPart textPart = new MimeBodyPart();
            String mailtext = "This is a Testmail";

            filePart.setDataHandler(new DataHandler(fileDataSource));
            filePart.setFileName("File.pdf");
            filePart.setHeader("Content-Transfer-Encoding", "base64");
            filePart.setHeader("Content-Type", "application/pdf");
            content.addBodyPart(filePart);

            textPart.setText(mailtext);
            textPart.setHeader("MIME-Version" , "1.0");
            textPart.setHeader("Content-Type" , textPart.getContentType());
            content.addBodyPart(textPart);

            Message msg = new MimeMessage(sess);
            InternetAddress addressFrom = new InternetAddress("sendermail@gmail.com");
            InternetAddress addressTo = new InternetAddress("recipientmail@gmail.com");
            InternetAddress adressCC = new InternetAddress("ccmail@gmail.com");

            msg.setFrom(addressFrom);
            msg.setRecipient(Message.RecipientType.TO , addressTo);
            msg.setRecipient(Message.RecipientType.CC, adressCC);
            msg.setSubject("Your file");                
            msg.setContent(content);

            Transport.send(msg);
            properties.clear();
        }
        catch(Exception ex1)
        {
            System.out.println(ex1);
        }
    }
    catch(Exception ex2)
    {
        System.out.println(ex2);
    }

我会感谢任何指导

1 个答案:

答案 0 :(得分:1)

正如mkl已经指出的那样,在使用临时文件发送邮件之前,应该关闭PdfStamper实例。现在,根据您共享的代码段,您的PDF文件损坏是正常的:您使用的是未完成的PDF。

除此之外,您使用临时文件是非常奇怪的。你为什么不在内存中创建文件?做这样的事情:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, baos);
// do stuff with stamper
stamper.close();
byte[] pdf_bytes = baos.toByteArray();

现在您可以在JavaMail中使用这些字节,如下所述:Send Email Attachement using byte[] with Java-Mail