本文介绍了使用JavaMail创建带附件的EML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用JavaMail创建带附件的EML文件。

I'll create a EML file with an attachment using JavaMail.

我成功创建了一个简单的EML文件,但添加附件无法正常工作。我要添加一个PDF文件。我的EML文件将成功创建。如果我用Outlook打开生成的EML文件,我将找不到我的PDF文件作为附件,但我会发现EML文件本身作为附件。有没有人有想法?

I created a simple EML file successfully, but adding an attachment don't work properly. I'm going to add a PDF file. My EML file will be created successfully. If I open the generated EML file with Outlook I'll find not my PDF file as attachment but I'll find the EML file itself as attachment. Does anyone has an idea?

我尝试了两个变种(结果相同),我使用 FileDataSource 类和<$的简单方法C $ C> MimeBodyPart的#attachFile(文件)。

I tried two variants (with same result), I used the FileDataSource class and the simple way with MimeBodyPart#attachFile(File).

我要发布一个例子:

File pdfFile = new File("somePdfFile");

Properties p = System.getProperties();
Session session = Session.getInstance(p);
MimeMessage message = new MimeMessage(session);
// MimeBodyPart txt = new MimeBodyPart();
// txt.setText("");
MimeBodyPart mbp = new MimeBodyPart();
mbp.attachFile(attachment);
// FileDataSource fds = new FileDataSource(attachment);
// fds.setFileTypeMap(new FileTypeMap() {
//    
//   @Override
//   public String getContentType(String arg0) {
//     return "application/pdf";
//   }
//    
//    @Override
//    public String getContentType(File file) {
//      return "application/pdf";
//    }
//      
//  });
//  mbp.setDataHandler(new DataHandler(fds));
//  mbp.setFileName("\"" + attachment.getName() + "\"");
//  mbp.setDisposition(MimePart.ATTACHMENT);
//  mbp.setHeader("Content-ID", "Attachment");
Multipart mp = new MimeMultipart();
//  mp.addBodyPart(txt);
mp.addBodyPart(mbp);
message.setContent(mp);
File emlFile = new File("message.eml");
emlFile.createNewFile();
message.writeTo(new FileOutputStream(emlFile));

// do something with the EML file
// Desktop.getDesktop().open(emlFile);

感谢您的回复。我上传了一个PDF文件(我用于测试,它是一个用Crystal Reports生成的简单HelloWorld)和生成的EML文件,应该包含PDF文件。

Thank you for your response. I uploaded a PDF file (that I use for testing, it's a simple HelloWorld generated with Crystal Reports) and the generated EML file which should include the PDF file.

我只是注意到如果我用Apple Mail或Outlook Express打开链接的EML文件,它可以工作(但没有编辑可能性)。也许这是Microsoft Outlook的问题?

I just noticed that if I open the linked EML file with Apple Mail or with Outlook Express it works (but without edit possibility). Maybe it's an issue of Microsoft Outlook?

链接被删除

推荐答案

您应该尝试将我提到的标题行添加到邮件的最顶部,然后查看Outlook如何处理它。添加收件人:来自:主题:和甚至可能是日期:,其中包含真实数据,而Outlook更有可能将其视为一条消息,而不仅仅是一个文件。

You should try adding the header lines I mentioned to the very top of the message and see how Outlook deals with it then. Add a To:, From:, Subject: and maybe even a Date: with real data in them, and Outlook is more likely to treat it as a message, rather that just a file.

这篇关于使用JavaMail创建带附件的EML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:04