Spring Framework : A Framework from SpringSource :: Spring with JavaMail
- Spring JavaMail Concepts
- Sending JavaMail
- Sending Attachment and Inline Resource
- Spring JavaMail Example1
- Download Examples
Spring JavaMail Concepts |
The Spring Framework provides a helpful utility library for sending email that enable 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.
- 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.MimeMessagePreparator
Library dependencies
The 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 JavaMail mail.jar library
- The JAF activation.jar library
Sending JavaMail |
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 having to use the verbose JavaMail API. Using the MimeMessageHelper it is pretty easy to create a MimeMessage.
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo("test@host.com");
helper.setText("Thank you for your order!");
sender.send(message);
Sending Attachment and Inline Resource |
Sending attachments and inline resources
Attachment
Multipart email messages allow for both attachments and inline resources. Examples of inline resources would be be images or a stylesheet you want to use in your message, but that you don’t want displayed as an attachment.
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 MimeMessageHelper(message, true);
helper.setTo("testing@host.com");
helper.setText("Check out the image!");
// let's attach the infamous windows Sample file (this time copied to c:/)
FileSystemResource file = new FileSystemResource(new File("c:/SampleImage.jpg"));
<em>helper.addAttachment("MyPics.jpg", file);</em>
sender.send(message);
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 MimeMessageHelper(message, true);
helper.setTo("test@host.com");
// use the true flag to indicate the text included is HTML
helper.setText("<html><body><img src='cid:identifier12345'></body></html>", true);
// let's include the infamous windows Sample file (this time copied to c:/)
FileSystemResource res1 = new FileSystemResource(new File("c:/SamplePics.jpg"));
<em>helper.addInline("identifier12345", res1);</em>
sender.send(message);
Spring JavaMail Example1 |
Click Below to download the Examples
Recent Comments