JavaMail PDF附件显示为乱码文本或编码文本

时间:2011-08-18 22:16:14

标签: javamail

我正在尝试通过JavaMail发送PDF附件,但是当我收到电子邮件时,我收到这样的内容,邮件中没有附件:

    ===EMAIL CONTENT===
    ------=_Part_0_3786439.1313701770148
    Content-Type: application/pdf
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename=ems_report.pdf

    JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9MZW5ndGggMTE4Ni9GaWx0ZXIvRmxhdGVEZWNvZGU+
    PnN0cmVhbQp4nLWaTW/jNhCG7/oVBIoF0kMYfgw/1FviZNsEa+/WVrCHogc3VgIXsb1VnS7670vK
    lhynMjmxRQSOBA1JPXpfaTSSyAgn55wworlw/x8W2V/ZVZFJTZTRpJhlN0X2aybInd/q2zHStC4W
    2cVHTvzaY3b2Y/Gnb7trUg/G6pXqKROMqFy4QRU5l25LmT2+HtFtUoZRadpRrR/VLd2f6352M5yQ
    8ctyWq1eljMyWC2+Pc+ny4eSjMtvq2pNHlcVGd0WNx6DkacOlN9+d8tZfSjtsNttb1q+4sqBKG6p
    5S0XeK72sM52TLfL+Xo+Xa+qv8nkZbGYVv8iWfYFozlwd/CvlpbJ3O/LszBLOGOdEnJmqLUEbE7l
    zhxW41Ln7fa3D/1xXi0OYwry3UXutv21C4x/bti3NIuMb9ees4nr33Tw5JvfXhcwTRe/5rscKwUY
    s5FCdEghBdWcgJYdUgBpfl6Kd5wzWDgeoROOzhuFoDPG5D3TQYROSUsBUHQSemZTETYNnAqBYhM9
    o4kImnyHbLpnNhlhA+HYJIrNLTX5kOCCCGUuwTSFnECuKJfhzPW5mpUuwY7Lh3L+TzlrQPcuLTeS
    lVT5gd52L1br6fPhowunL96mPIFOebzNeeLUnAcRFRVzSQ8l4i79b+R8q6KSLtnrwyp+mk//eC6P
    lRFaGRVaRmhlVKfKqCIyaiejwp2MMR214PV5fUjH0Wp5/nySlqrVUqO1VK2W+lQtRURL2dspKVNe


===CODE===


MimeMultipart multipart = new MimeMultipart();
     MimeBodyPart part1 = new MimeBodyPart();
     FileDataSource source = new FileDataSource("ems_report.pdf");

     part1.setDataHandler(new DataHandler(source));
     part1.setFileName(source.getName());
     part1.setHeader("Content-Type", "application/pdf");
     part1.setHeader("Content-Transfer-Encoding", "base64");
     part1.setDisposition(Part.ATTACHMENT);

     multipart.addBodyPart(part1);

     MimeBodyPart part2 = new MimeBodyPart();
     part2.setText("mimebodypart part2");

     multipart.addBodyPart(part2);
     message.setContent(multipart);
     message.setHeader("Content-Type", "multipart/mixed");

我在这里做错了什么?我已经google了一整天,似乎没有人有解决方案。非常感谢任何帮助,非常感谢!

1 个答案:

答案 0 :(得分:1)

您所看到的是多部分的二进制数据。首先必须解码base64字符串。之后,您可以处理PDF。

PS:如果您在客户端(Webmail,Outlook,Thunderbird)中打开该邮件,您应该正确看到PDF。