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

发布于 2024-07-06 15:34:43 字数 73 浏览 9 评论 0原文

有人知道该怎么做吗? 我获取了电子邮件的所有信息(正文、主题、发件人、收件人、抄送、密件抄送),并且需要从中生成 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

转身以后 2024-07-13 15:34:43

您可以使用以下代码创建 eml 文件。 它可以与雷鸟以及其他电子邮件客户端一起正常工作:

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);
    }
}

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

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);
    }
}
我的影子我的梦 2024-07-13 15:34:43

您可以构造 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 use writeTo() method to save it to file.
See JavaMail API for more information.

拥抱没勇气 2024-07-13 15:34:43

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

有关更多信息,官方规范为 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:

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.

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.

一场春暖 2024-07-13 15:34:43

查看典型的 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.

她说她爱他 2024-07-13 15:34:43

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

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

(正如 Marco Sulla 所说),但也要更改

message.setContent(multipart);

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

If you want to add HTML Stuff you have to add

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

(as Marco Sulla said) but also change

message.setContent(multipart);

to

message.setContent(multipart,"text/html");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文