Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading .eml files into javax.mail.Messages

I'm trying to unit test a method which processes javax.mail.Message instances.

I am writing a converter to change emails which arrive in different formats and are then converted into a consistent internal format (MyMessage). This conversion will usually depend on the from-address or reply-address of the email, and the parts of the email, the subject, and the from- and reply-addresses will be required for creating the new MyMessage.

I have a collection of raw emails which are saved locally as .eml files, and I'd like to make a unit test which loads the .eml files from the classpath and converts them to javax.mail.Message instances. Is this possible, and if so, how would it be done?

like image 507
Armand Avatar asked May 06 '10 14:05

Armand


People also ask

How do I attach a .EML file to an email?

You can save email files by dragging them to your desktop, a composer window in Front (Windows desktop app only), or another app. You can click on the email in your conversation list or on a specific message in your open conversation and drag it out of Front to save it as an . eml file or attach it in another app.

What program opens EML files?

You can open EML files with a variety of email programs, such as Microsoft Outlook (Windows), Apple Mail (macOS), and Mozilla Thunderbird (multiplatform).

Can I import an EML file into Gmail?

You can import EML files to Gmail by configuring the Gmail account in an email client that supports EML files with account login credentials. This method is considered as the most secured & highly reliable way to do EML to Gmail migration.


1 Answers

After a few tests, I finally successfully loaded a message using the MimeMessage(Session, InputStream) public constructor (as opposed to the Folder-based protected one cited in the other response).

import java.io.FileInputStream;
import java.io.InputStream;

import javax.mail.internet.MimeMessage;

public class LoadEML {

    public static void main(String[] args) throws Exception {
        InputStream is = new FileInputStream(args[0]);
        MimeMessage mime = new MimeMessage(null, is);
        System.out.println("Subject: " + mime.getSubject());
    }

}
like image 163
lapo Avatar answered Oct 16 '22 04:10

lapo