java 使用 JavaMail 创建带有附件的 EML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/421944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creation of a EML file with an attachment using JavaMail
提问by Thomas Zuberbühler
I'll create a EML file with an attachment using JavaMail.
我将使用 JavaMail 创建一个带有附件的 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?
我成功创建了一个简单的 EML 文件,但添加附件无法正常工作。我要添加一个PDF文件。我的 EML 文件将成功创建。如果我用 Outlook 打开生成的 EML 文件,我会发现不是我的 PDF 文件作为附件,但我会发现 EML 文件本身作为附件。有没有人有想法?
I tried two variants (with same result), I used the FileDataSourceclass and the simple way with MimeBodyPart#attachFile(File).
我尝试了两种变体(结果相同),我使用了FileDataSource类和MimeBodyPart#attachFile(File).
I'm going to post an example:
我将发布一个示例:
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);
Create a .eml (email) file in Java
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.
感谢您的答复。我上传了一个 PDF 文件(我用于测试,它是一个用 Crystal Reports 生成的简单 HelloWorld)和生成的 EML 文件,其中应包含 PDF 文件。
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?
我只是注意到,如果我用 Apple Mail 或 Outlook Express 打开链接的 EML 文件,它就可以工作(但不能进行编辑)。也许这是 Microsoft Outlook 的问题?
The links are removed
链接已删除
采纳答案by LarryF
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.
您应该尝试将我提到的标题行添加到邮件的最顶部,然后看看 Outlook 如何处理它。添加To:, From:,Subject:甚至可能Date:包含真实数据的 ,Outlook 更有可能将其视为消息,而不仅仅是文件。
回答by LarryF
Zubi, it looks like the issue is the content type on the attachment is set to "application/octet-stream". So, it looks like the mail reader is taking the PDF file as an alternate display for the "text" body of the message which does not exist, (it's just blank).
Zubi,问题似乎是附件上的内容类型设置为“application/octet-stream”。因此,看起来邮件阅读器将 PDF 文件作为不存在的邮件“文本”正文的替代显示(它只是空白)。
You'll have to forgive me, it's been more than a year since I've dealt with Mime, but I think you are going to want to A) Put some body text in the message, B) Make sure the type on the attachment is set to application/pdf. Hopefully, this will prevent the mail reading from trying to display the PDF as the primary body of the message.
你必须原谅我,自从我和 Mime 打交道已经一年多了,但我想你会想要 A) 在消息中放一些正文,B) 确保附件上的类型设置为应用程序/pdf。希望这将阻止邮件阅读尝试将 PDF 显示为邮件的主要正文。
Other than that, it looks normal... Now, Outlook MIGHT bitch because there are no RFC 822 headers in the main body. You may want to add at LEAST a From:, To:, and a Subject:.
除此之外,它看起来很正常......现在,Outlook 可能是婊子,因为主体中没有 RFC 822 标头。您可能需要添加至少一个From:,To:和一个Subject:。
The message passed MY MIME parsing code...
消息通过了我的 MIME 解析代码...

