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?
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.
You can open EML files with a variety of email programs, such as Microsoft Outlook (Windows), Apple Mail (macOS), and Mozilla Thunderbird (multiplatform).
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.
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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With