EmailIntroductionLibrary dependenciesThe following additional jars to be on the classpath of your
application in order to be able to use the Spring Framework's email library.The JavaMailmail.jar libraryThe JAFactivation.jar libraryAll of these libraries are freely available on the web.The Spring Framework provides a helpful utility library for sending
email that shields the user from the specifics of the underlying mailing
system and is responsible for low level resource handling on behalf of
the client.The org.springframework.mail package is the root level package
for the Spring Framework's email support. The central interface for sending
emails is the MailSender interface; a simple value object
encapsulating the properties of a simple mail such as from and
to (plus many others) is the SimpleMailMessage class.
This package also contains a hierarchy of checked exceptions which provide
a higher level of abstraction over the lower level mail system exceptions
with the root exception being MailException. Please
refer to the JavaDocs for more information on the rich mail exception hierarchy.The org.springframework.mail.javamail.JavaMailSender
interface adds specialized JavaMail features such as MIME
message support to the MailSender interface
(from which it inherits). JavaMailSender also provides a
callback interface for preparation of JavaMail MIME messages, called
org.springframework.mail.javamail.MimeMessagePreparatorUsageLet's assume there is a business interface called OrderManager:Let us also assume that there is a requirement stating that an email message
with an order number needs to be generated and sent to a customer placing the
relevant order.Basic MailSender and SimpleMailMessage usage// Do the business calculations...// Call the collaborators to persist the order...// Create a thread safe "copy" of the template message and customize it// simply log it and go on...Find below the bean definitions for the above code:
]]><!-- this is a template message that we can pre-load with default state -->]]>Using the JavaMailSender and the MimeMessagePreparatorHere is another implementation of OrderManager using
the MimeMessagePreparator callback interface. Please note
in this case that the mailSender property is of type
JavaMailSender so that we are able to use the JavaMail
MimeMessage class:// Do the business calculations...// Call the collaborators to persist the order...// simply log it and go on...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.Using the JavaMail MimeMessageHelperA class that comes in pretty handy when dealing with JavaMail messages is
the org.springframework.mail.javamail.MimeMessageHelper class,
which shields you from 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 casesSending attachments and inline resourcesMultipart 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.AttachmentsThe following example shows you how to use the
MimeMessageHelper to send an email along with a
single JPEG image attachment.// use the true flag to indicate you need a multipart message// let's attach the infamous windows Sample file (this time copied to c:/)Inline resourcesThe following example shows you how to use the
MimeMessageHelper to send an email along with an
inline image.// use the true flag to indicate you need a multipart message// use the true flag to indicate the text included is HTML