Search code examples
javajakarta-mailmailto

Preview an email in default mail client before sending with Java and MimeMessage


How can i send a MimeMessage to the default email client so i can preview it before sending in JavaMail?

I work at a very burocratic company and this is a MUST.


Solution

  • If the total message size is small and plain text you can call MimeMessage.saveChanges then call all of the get methods build a URI from the existing MimeMessage. Then use the java.awt.Desktop.mail(URI) method to launch email client.

    Otherwise, you have to call MimeMessage.saveChanges then use MimeMessage.writeTo to save it to the filesystem as '.eml'. Then open that file with java.awt.Desktop.open to launch the email client. You'll have to handle clean up after the email client is closed.

    You also have to think about the security implications of email messages being left on the file system.