用 Java 创建一个 .eml(电子邮件)文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/157195/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:52:13  来源:igfitidea点击:

Create a .eml (email) file in Java

javaemailoutlookeml

提问by Jan Gressmann

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.

有谁知道如何做到这一点?我获得了电子邮件的所有信息(正文、主题、发件人、收件人、抄送、密件抄送),需要从中生成一个 .eml 文件。

采纳答案by Lazarin

You can construct javax.mail.Messageobject (or have it already constructed from the mail server) and then you can use writeTo()method to save it to file. See JavaMail APIfor more information.

您可以构造javax.mail.Message对象(或者已经从邮件服务器构造了它),然后您可以使用writeTo()方法将其保存到文件中。有关更多信息,请参阅JavaMail API

回答by Neall

EML files are just plain text files. The headers are separated from the body by a blank line. Headers look like this:

EML 文件只是纯文本文件。标题与正文由一个空行分隔。标题看起来像这样:

From: "DR CLEMENT OKON" <[email protected]>
To: "You" <[email protected]>
Subject: REQUEST FOR URGENT BUSINESS RELATIONSHIP 
Date: Tue, 30 Sep 2008 09:42:47 -0400

For more info, the official spec is RFC 2822. It's actually not as hard to read as some RFCs.

有关更多信息,官方规范是RFC 2822。它实际上并不像某些 RFC 那样难以阅读。

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.

编辑:当我说“纯文本”时,我应该考虑一下。我的意思是纯 ASCII - 而不是 8 位“扩展 ASCII” - 最多字符 127。如果你想要超过 7 位,你需要某种编码,事情会变得复杂。

回答by Hristo Deshev

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.

查看一个典型的 EML 文件,它看起来像是发送到服务器的文本通信的原始转储。所以它是一个包含邮件标题和正文的文本文件。要在 EML 文件中以正确的格式获取附件、不同的视图等,您需要对正文及其部分进行 MIME 编码。

回答by salocinx

You can create eml files with the following code. It works fine with thunderbird and probably with other email clients:

您可以使用以下代码创建 eml 文件。它适用于 Thunderbird,也可能适用于其他电子邮件客户端:

public static void createMessage(String to, String from, String subject, String body, List<File> attachments) {
    try {
        Message message = new MimeMessage(Session.getInstance(System.getProperties()));
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject(subject);
        // create the message part 
        MimeBodyPart content = new MimeBodyPart();
        // fill message
        content.setText(body);
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(content);
        // add attachments
        for(File file : attachments) {
            MimeBodyPart attachment = new MimeBodyPart();
            DataSource source = new FileDataSource(file);
            attachment.setDataHandler(new DataHandler(source));
            attachment.setFileName(file.getName());
            multipart.addBodyPart(attachment);
        }
        // integration
        message.setContent(multipart);
        // store file
        message.writeTo(new FileOutputStream(new File("c:/mail.eml")));
    } catch (MessagingException ex) {
        Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex);
    }
}

回答by BamBouZ

If you want to add HTML Stuff you have to add

如果你想添加 HTML 东西,你必须添加

content.setHeader("Content-Type", "text/html"); 

(as Marco Sulla said) but also change

(正如马可苏拉所说)但也要改变

message.setContent(multipart);

to

message.setContent(multipart,"text/html");