Sending an email with attachments
In the following example, we are going to show how to send an email message with an attachment. This is done by using what's called a multi-part message, which is an extension to the mail protocol to include messages with different parts, where one or more of them are file parts with binary data, rather than regular text. The concept of multi-part in the web is very popular in associating files with a specific request. Remember that we have used multi-part-style requests in the JAX-RS chapter, to upload a file to a RESTful service:
.... Message message = new MimeMessage(session); // filling mail attributes message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject("Mail Subject"); // creeate a multipart content object Multipart multipart = MimeMultipart(); // create a message part for mail body BodyPart...