How to create a .EML file in Java

1090415 pts.
Tags:
.EML file
Java
Microsoft Outlook
How would I be able to create a .EML file in Java? I already have the information of the email (through Outlook) including body, subject, from, to, etc. Now I need to generate an .EML file from all of it. Thanks!

Answer Wiki

Thanks. We'll let you know when a new response is added.
EML stands for e-mail.  EML is a file extension that is used by Microsoft’s Outlook Express.  It implements the MIME RFC 822 standard and has been around since 1982.  The text of such a file exists as ASCII in both the header and body.  It also includes information regarding the receiver, sender, date and time.
 
You can create an EML file by using the “save as” feature in many Windows compatible e-mail clients such as Thunderbird.  You can also create your own EML file with Java. Here’s some code that should work:
 
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);
    }
}
 
Once created, the file can be opened in any e-mail program that is compatible with the MIME RFC 822 standard.  If you have trouble opening the file with your e-mail client, consider using a third party program specifically designed for opening EML files such as EML Viewer Pro.  

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: