Using the JavaMail MimeMessageHelper

4.3.9.RELEASE Spring Framework 752 import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessagePreparator; public class SimpleOrderManager implements OrderManager { private JavaMailSender mailSender; public void setMailSenderJavaMailSender mailSender { this .mailSender = mailSender; } public void placeOrder final Order order { Do the business calculations... Call the collaborators to persist the order... MimeMessagePreparator preparator = new MimeMessagePreparator { public void prepareMimeMessage mimeMessage throws Exception { mimeMessage.setRecipientMessage.RecipientType.TO, new InternetAddressorder.getCustomer.getEmailAddress; mimeMessage.setFrom new InternetAddress mailmycompany.com ; mimeMessage.setText Dear + order.getCustomer.getFirstName + + order.getCustomer.getLastName + , thank you for placing order. Your order number is + order.getOrderNumber; } }; try { this .mailSender.sendpreparator; } catch MailException ex { simply log it and go on... System.err.printlnex.getMessage; } } } Note The mail code is a crosscutting concern and could well be a candidate for refactoring into a custom Spring AOP aspect , which then could be executed at appropriate joinpoints on the OrderManager target. The Spring Framework’s mail support ships with the standard JavaMail implementation. Please refer to the relevant javadocs for more information.

33.3 Using the JavaMail MimeMessageHelper

A class that comes in pretty handy when dealing with JavaMail messages is the org.springframework.mail.javamail.MimeMessageHelper class, which shields you from 4.3.9.RELEASE Spring Framework 753 having to use the verbose JavaMail API. Using the MimeMessageHelper it is pretty easy to create a MimeMessage : of course you would use DI in any real-world cases JavaMailSenderImpl sender = new JavaMailSenderImpl; sender.setHost mail.host.com ; MimeMessage message = sender.createMimeMessage; MimeMessageHelper helper = new MimeMessageHelpermessage; helper.setTo testhost.com ; helper.setText Thank you for ordering ; sender.sendmessage; Sending attachments and inline resources Multipart email messages allow for both attachments and inline resources. Examples of inline resources would be images or a stylesheet you want to use in your message, but that you don’t want displayed as an attachment. Attachments The following example shows you how to use the MimeMessageHelper to send an email along with a single JPEG image attachment. JavaMailSenderImpl sender = new JavaMailSenderImpl; sender.setHost mail.host.com ; MimeMessage message = sender.createMimeMessage; use the true flag to indicate you need a multipart message MimeMessageHelper helper = new MimeMessageHelpermessage, true; helper.setTo testhost.com ; helper.setText Check out this image ; lets attach the infamous windows Sample file this time copied to c: FileSystemResource file = new FileSystemResource new File c:Sample.jpg ; helper.addAttachment CoolImage.jpg , file; sender.sendmessage; Inline resources The following example shows you how to use the MimeMessageHelper to send an email along with an inline image. JavaMailSenderImpl sender = new JavaMailSenderImpl; sender.setHost mail.host.com ; MimeMessage message = sender.createMimeMessage; use the true flag to indicate you need a multipart message MimeMessageHelper helper = new MimeMessageHelpermessage, true; helper.setTo testhost.com ; use the true flag to indicate the text included is HTML helper.setText htmlbodyimg src=cid:identifier1234bodyhtml , true; lets include the infamous windows Sample file this time copied to c: FileSystemResource res = new FileSystemResource new File

c:Sample.jpg ;