bbottema/simple-java-mail

Simple Java Mail from MimeMessage

neil-ceschi opened this issue · 5 comments

I would need to handle files saved in .eml format, easily convertible into MimeMessage. It would be great to build SimpleMail from MimeMessage!
Thanks a lot!

If you provide an implementation or piece of code that fills an Email instance completely using a MimeMessage, I will integrate it into Simple Email.

The truth is, MimeMessage is a ambiguous type tree structure. The Email class works exactly why MimeMessage doesn't: it has a simple interface and it allows you do define an email in exactly one way. MimeMessage on the other hand is a messy heap of recursive nonsense and extracting data from that is a lot harder than putting data into it.

On the other hand, Apache seems to have an implementation already. I might just use some of that.

@neil-ceschi, I implemented a MimeMessage parser based on Apache's version. I had to modify a few a few things: headers were not parsed in Apache's version and attachments and embedded images were not properly distinguished (they ignored Part.INLINE and Part.ATTACHMENT predispositions).

I'll release a new version somewhere this week when I have the time. Releasing always takes me more time than implementing an actual fix :/

Usage:

Email email = new Email(yourMimeMessage);
// the other way around is now possible too!
MimeMessage mimeMessage = Mailer.produceMimeMessage(yourEmail, yourSession /* or null */);

The following should be true then:

Email emailSource = new Email();
/* populate email... */
Email emailTarget = new Email(mailer.produceMimeMessage(emailSource, null));

emailSource.equals(emailTarget);

Release in v3.0.0!

Demo included in MailTest.java