jakarta-mailattachmentmailto

How to open mail in draft and attach file to mail?


I was trying to send a file using mail but I wanted to open it first in a draft mail like mailto in jsp. I had implemented mail to functionality in Java but I am not able to attach file to opened mail. Others are working fine except the attachment.

public static void mailto(List<String> recipients, String subject, String body) 
    throws IOException, URISyntaxException {
  String uriStr = String.format("mailto:%s?subject=%s&body=%s", recipients, subject, body);
  Desktop.getDesktop().browse(new URI(uriStr));
}

How can I attach a file using mailto or any other APIs?


Solution

  • Create Multipart MimeMessage using JavaMail but instead sending it 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.