使用 Java 创建 .eml(电子邮件)文件
有人知道该怎么做吗? 我获取了电子邮件的所有信息(正文、主题、发件人、收件人、抄送、密件抄送),并且需要从中生成 .eml 文件。
Anybody knows how to do this? I got all the information of the email (body, subject, from , to, cc, bcc) and need to generate an .eml file out of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用以下代码创建 eml 文件。 它可以与雷鸟以及其他电子邮件客户端一起正常工作:
You can create eml files with the following code. It works fine with thunderbird and probably with other email clients:
您可以构造
javax.mail.Message
对象(或者已经从邮件服务器构造了它),然后您可以使用
writeTo()
方法将其保存到文件。有关详细信息,请参阅 JavaMail API。
You can construct
javax.mail.Message
object (or have it already constructed from the mail server) and then you can usewriteTo()
method to save it to file.See JavaMail API for more information.
EML 文件只是纯文本文件。 标题与正文之间用空行分隔。 标头如下所示:
有关更多信息,官方规范为 RFC 2822。 它实际上并不像某些 RFC 那样难以阅读。
编辑:当我说“纯文本”时,我应该想一想。 我真正的意思是纯 ASCII - 而不是 8 位“扩展 ASCII” - 最多是字符 127。如果您想要超过 7 位,则需要某种编码,事情就会变得复杂。
EML files are just plain text files. The headers are separated from the body by a blank line. Headers look like this:
For more info, the official spec is RFC 2822. It's actually not as hard to read as some RFCs.
Edit: When I said "plain text" I should have thought for a second. I really meant plain ASCII - and not the 8-bit "extended ASCII" either - just up to character 127. If you want more than seven bits, you need some kind of encoding and things get complicated.
查看典型的 EML 文件,它看起来像是发送到服务器的文本通信的原始转储。 所以它是一个包含邮件标题和正文的文本文件。 为了在 EML 文件中以正确的格式获取附件、不同的视图等,您需要对正文及其部分进行 MIME 编码。
Looking at a typical EML file it looks like a raw dump of the text communication that went to the server. So it is a text file containing the mail headers and body. To get your attachments, different views, etc in the correct format inside the EML file you need to MIME-encode the body and its parts.
如果你想添加 HTML 东西,你必须添加
(正如 Marco Sulla 所说),但也要更改
为
If you want to add HTML Stuff you have to add
(as Marco Sulla said) but also change
to