简体   繁体   English

用Java创建一个.eml(email)文件

[英]Create a .eml (email) file in Java

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. 我收到了电子邮件的所有信息(正文,主题,从,到,cc,密送),需要生成一个.eml文件。

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. 您可以构造javax.mail.Message对象(或者已经从邮件服务器构造它),然后您可以使用writeTo()方法将其保存到文件。 See JavaMail API for more information. 有关更多信息,请参阅JavaMail API

You can create eml files with the following code. 您可以使用以下代码创建eml文件。 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);
    }
}

EML files are just plain text files. EML文件只是纯文本文件。 The headers are separated from the body by a blank line. 标题通过空行与主体分开。 Headers look like this: 标题看起来像这样:

From: "DR CLEMENT OKON" <drclement@nigerianspam.com>
To: "You" <you@yourdomain.com>
Subject: REQUEST FOR URGENT BUSINESS RELATIONSHIP 
Date: Tue, 30 Sep 2008 09:42:47 -0400

For more info, the official spec is RFC 2822 . 有关更多信息,官方规范是RFC 2822 It's actually not as hard to read as some RFCs. 它实际上并不像某些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位,你需要某种编码,事情变得复杂。

Looking at a typical EML file it looks like a raw dump of the text communication that went to the server. 查看典型的EML文件,它看起来像是转发到服务器的文本通信的原始转储。 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文件中以正确的格式获取附件,不同视图等,您需要对主体及其部件进行MIME编码。

If you want to add HTML Stuff you have to add 如果要添加HTML Stuff,则必须添加

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

(as Marco Sulla said) but also change (正如Marco Sulla所说)但也有所改变

message.setContent(multipart);

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM