Sending Email with Spring mail abstraction layer
使用Spring郵件抽象層發(fā)送郵件:
18.1. Introduction
介紹
Spring provides a higher level of abstraction for sending electronic mail which shields the user from the specifics of underlying mailing system and is responsible for a low level resource handling on behalf of the client.
Spring 支持一個(gè)更高層的抽象用來(lái)發(fā)送電子郵件,它隱藏底層郵件系統(tǒng)的細(xì)節(jié)并且代表客戶端對(duì)低級(jí)別的控制 。
18.2. Spring mail abstraction structure
Spring郵件抽象結(jié)構(gòu)
The main package of Spring mail abstraction layer is org.springframework.mail package. It contains central interface for sending emails called MailSender and the value object which encapsulates properties of a simple mail such as from, to, cc, subject, text called SimpleMailMessage. 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 JavaDocs for more information on mail exception hierarchy.
Sring郵件抽象層的主要包是:org.springframework.mail 包。它包含叫MailSender為發(fā)送郵件的核心接口和包含簡(jiǎn)單郵件屬性例如from,to,cc,subject,text叫SimpleMailMessage的值對(duì)象. 這個(gè)包也包含一個(gè)檢查異常的層次,它支持一個(gè)更高級(jí)別的抽象超過(guò)低級(jí)別的郵件系統(tǒng)異常伴隨根異常存在MailException. 請(qǐng)參考JavaDocs為更多的信息雜郵件異常層次。
Spring also provides a sub-interface of MailSender for specialized JavaMail features such as MIME messages, namely org.springframework.mail.javamail.JavaMailSender It also provides a callback interface for preparation of JavaMail MIME messages, namely org.springframework.mail.javamail.MimeMessagePreparator
Spring也支持一個(gè)MailSender的專用于JavaMail特征例如MIME消息子接口,命名為org.springframework.javamail.JavaMailerSener。它也支持一個(gè)為JavaMail MIME信息的準(zhǔn)備回調(diào)接口,命名為org.springframework.mail.JavaMail.MimeMessagePreparator.
MailSender:
public interface MailSender {
/**
* Send the given simple mail message.
* @param simpleMessage message to send
* @throws MailException in case of message, authentication, or send errors
* 發(fā)送給定的簡(jiǎn)單郵件信息
* @參數(shù) simpleMessage 發(fā)送的信息
* @throws MailException 假設(shè)信息,證明或發(fā)送錯(cuò)誤
*/
public void send(SimpleMailMessage simpleMessage) throws MailException;
/**
* Send the given array of simple mail messages in batch.
* @param simpleMessages messages to send
* @throws MailException in case of message, authentication, or send errors
*/
public void send(SimpleMailMessage[] simpleMessages) throws MailException;
}
JavaMailSender:
public interface JavaMailSender extends MailSender {
/**
* Create a new JavaMail MimeMessage for the underlying JavaMail Session
* of this sender. Needs to be called to create MimeMessage instances
* that can be prepared by the client and passed to send(MimeMessage).
* @return the new MimeMessage instance
* @see #send(MimeMessage)
* @see #send(MimeMessage[])
* 創(chuàng)建一個(gè)新的JavaMail MimeMessage 為潛在的JavaMail的發(fā)送者的會(huì)話.
* 需要被調(diào)用來(lái)創(chuàng)建MimeMessage實(shí)例,它可以被客戶準(zhǔn)備并且被傳遞發(fā)送(MimeMessage).
* @return 這個(gè)新的MimeMessage 實(shí)例
* @see #send(Message)
* @sess #send(MimeMessage[])
*/
public MimeMessage createMimeMessage();
/**
* Send the given JavaMail MIME message.
* The message needs to have been created with createMimeMessage.
* @param mimeMessage message to send
* @throws MailException in case of message, authentication, or send errors
* @see #createMimeMessage
*/
public void send(MimeMessage mimeMessage) throws MailException;
/**
* Send the given array of JavaMail MIME messages in batch.
* The messages need to have been created with createMimeMessage.
* @param mimeMessages messages to send
* @throws MailException in case of message, authentication, or send errors
* @see #createMimeMessage
*/
public void send(MimeMessage[] mimeMessages) throws MailException;
/**
* Send the JavaMail MIME message prepared by the given MimeMessagePreparator.
* Alternative way to prepare MimeMessage instances, instead of createMimeMessage
* and send(MimeMessage) calls. Takes care of proper exception conversion.
* @param mimeMessagePreparator the preparator to use
* @throws MailException in case of message, authentication, or send errors
*/
public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException;
/**
* Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.
* Alternative way to prepare MimeMessage instances, instead of createMimeMessage
* and send(MimeMessage[]) calls. Takes care of proper exception conversion.
* @param mimeMessagePreparators the preparator to use
* @throws MailException in case of message, authentication, or send errors
*/
public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException;
}
MimeMessagePreparator:
public interface MimeMessagePreparator {
/**
* Prepare the given new MimeMessage instance.
* @param mimeMessage the message to prepare
* @throws MessagingException passing any exceptions thrown by MimeMessage
* methods through for automatic conversion to the MailException hierarchy
*/
void prepare(MimeMessage mimeMessage) throws MessagingException;
}
18.3. Using Spring mail abstraction
使用Spring郵件抽象
Let's assume there is a business interface called OrderManager
讓我們假定這里有一個(gè)商業(yè)接口叫OrderManager
public interface OrderManager {
void placeOrder(Order order);
}
and there is a use case that says that an email message with order number would need to be generated and sent to a customer placing that order. So for this purpose we want to use MailSender and SimpleMailMessage
并且這里有一個(gè)有用案例,可以說(shuō)一個(gè)伴隨訂單編號(hào)的郵件信息將需要被產(chǎn)生并且發(fā)送給一個(gè)客戶處理這個(gè)訂單。所以為這個(gè)目的我們想要使用MailSender和SimpleMailSender.
Please note that as usual, we work with interfaces in the business code and let Spring IoC container take care of wiring of all the collaborators for us.
請(qǐng)注意照常,我們工作使用在商業(yè)代碼中的接口并且讓Spring Ioc 容器關(guān)心為我們的所有合作者。
Here is the implementation of OrderManager
這里是OrderManager的實(shí)現(xiàn):
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class OrderManagerImpl implements OrderManager {
private MailSender mailSender;
private SimpleMailMessage message;
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void setMessage(SimpleMailMessage message) {
this.message = message;
}
public void placeOrder(Order order) {
//... * Do the business calculations....
//... * Call the collaborators to persist the order
//Create a thread safe "sandbox" of the message
SimpleMailMessage msg = new SimpleMailMessage(this.message);
msg.setTo(order.getCustomer().getEmailAddress());
msg.setText(
"Dear "
+ order.getCustomer().getFirstName()
+ order.getCustomer().getLastName()
+ ", thank you for placing order. Your order number is "
+ order.getOrderNumber());
try{
mailSender.send(msg);
}
catch(MailException ex) {
//log it and go on
System.err.println(ex.getMessage());
}
}
}
Here is what the bean definitions for the code above would look like:
這里是這個(gè)為這個(gè)以上代碼bean定義類似:
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host"><value>mail.mycompany.com</value></property>
</bean>
<bean id="mailMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from"><value>customerservice@mycompany.com</value></property>
<property name="subject"><value>Your order</value></property>
</bean>
<bean id="orderManager"
class="com.mycompany.businessapp.support.OrderManagerImpl">
<property name="mailSender"><ref bean="mailSender"/></property>
<property name="message"><ref bean="mailMessage"/></property>
</bean>
Here is the implementation of OrderManager using MimeMessagePreparator callback interface. Please note that the mailSender property is of type JavaMailSender in this case in order to be able to use JavaMail MimeMessage:
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 OrderManagerImpl implements OrderManager {
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender 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 prepare(MimeMessage mimeMessage) throws MessagingException {
mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(order.getCustomer().getEmailAddress()));
mimeMessage.setFrom(new InternetAddress("mail@mycompany.com"));
mimeMessage.setText(
"Dear "
+ order.getCustomer().getFirstName()
+ order.getCustomer().getLastName()
+ ", thank you for placing order. Your order number is "
+ order.getOrderNumber());
}
};
try{
mailSender.send(preparator);
}
catch(MailException ex) {
//log it and go on
System.err.println(ex.getMessage());
}
}
}
If you want to use JavaMail MimeMessage to the full power, the MimeMessagePreparator is available at your fingertips.
如果你想使用JavaMail MimeMessage來(lái)使得足夠強(qiáng)大,MimeMessagePreparator 是可以利用的。
Please note that the mail code is a crosscutting(橫切的) concern(關(guān)注) and is a perfect candidate(候選) for refactoring into a custom Spring AOP advice, which then could easily be applied to OrderManager target. Please see the AOP chapter.
18.3.1. Pluggable MailSender implementations
Spring comes with two MailSender implementations out of the box - the JavaMail implementation and the implementation on top of Jason Hunter's MailMessage class that's included in http://servlets.com/cos (com.oreilly.servlet). Please refer to JavaDocs for more information.
18.4. Using the JavaMail MimeMessageHelper
One of the components that comes in pretty handy when dealing with JavaMail messages is the org.springframework.mail.javamail.MimeMessageHelper. It prevents you from having to use the nasty APIs the the javax.mail.internet classes. A couple of possible scenarios:
18.4.1. Creating a simple MimeMessage and sending it
Using the MimeMessageHelper it's pretty easy to setup and send a MimeMessage:
// of course you would setup the mail sender using
// DI in any real-world cases
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost("mail.host.com");
MimeMessage message = sender.createMimeMesage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo("test@host.com");
helper.setText("Thank you for ordering!");
sender.send(message);
18.4.2. Sending attachments and inline resources
Email allow for attachments, but also for inline resources in multipart messages. Inline resources could for example be images or stylesheet you want to use in your message, but don't want displayed as attachment. The following 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.createMimeMesage();
// 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:identifier1234'></body></html>"
true);
// let's include the infamous windows Sample file (this time copied to c:/)
FileSystemResource res = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addInline("identifier1234", res);
// if you would need to include the file as an attachment, use
// addAttachment() methods on the MimeMessageHelper
sender.send(message);
Inline resources are added to the mime message using the Content-ID specified as you've seen just now (identifier1234 in this case). The order in which you're adding the text and the resource are VERY important. First add the text and after that the resources. If you're doing it the other way around, it won't work!