Create a .eml (email) file in Java
Asked Answered
S

5

24

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.

Surveillance answered 1/10, 2008 at 11:33 Comment(0)
G
24

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.

Geny answered 1/10, 2008 at 13:7 Comment(2)
... package javax.mail does not existThorax
@Supuhstar - Old thread but for anyone else reading this, this relies on the javamail API.Carefree
G
28

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);
    }
}
Goofy answered 30/10, 2012 at 10:2 Comment(1)
To save an html message, add content.setHeader("Content-Type", "text/html");Subjectify
G
24

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.

Geny answered 1/10, 2008 at 13:7 Comment(2)
... package javax.mail does not existThorax
@Supuhstar - Old thread but for anyone else reading this, this relies on the javamail API.Carefree
B
9

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.

Brumby answered 1/10, 2008 at 11:48 Comment(0)
L
1

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.

Lochia answered 1/10, 2008 at 11:50 Comment(0)
O
0

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");
Otalgia answered 13/1, 2019 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.