1// of course you would use DI in any real-world cases
2JavaMailSenderImpl sender = new JavaMailSenderImpl();
3sender.setHost("mail.host.com");
4
5MimeMessage message = sender.createMimeMessage();
6MimeMessageHelper helper = new MimeMessageHelper(message);
7helper.setTo("test@host.com");
8helper.setText("Thank you for ordering!");
9
10sender.send(message);
1import javax.mail.Message;
2import javax.mail.MessagingException;
3import javax.mail.internet.InternetAddress;
4import javax.mail.internet.MimeMessage;
5
6import javax.mail.internet.MimeMessage;
7import org.springframework.mail.MailException;
8import org.springframework.mail.javamail.JavaMailSender;
9import org.springframework.mail.javamail.MimeMessagePreparator;
10
11public class SimpleOrderManager implements OrderManager {
12
13 private JavaMailSender mailSender;
14
15 public void setMailSender(JavaMailSender mailSender) {
16 this.mailSender = mailSender;
17 }
18
19 public void placeOrder(final Order order) {
20
21 // Do the business calculations...
22
23 // Call the collaborators to persist the order...
24
25 MimeMessagePreparator preparator = new MimeMessagePreparator() {
26
27 public void prepare(MimeMessage mimeMessage) throws Exception {
28
29 mimeMessage.setRecipient(Message.RecipientType.TO,
30 new InternetAddress(order.getCustomer().getEmailAddress()));
31 mimeMessage.setFrom(new InternetAddress("mail@mycompany.com"));
32 mimeMessage.setText(
33 "Dear " + order.getCustomer().getFirstName() + " "
34 + order.getCustomer().getLastName()
35 + ", thank you for placing order. Your order number is "
36 + order.getOrderNumber());
37 }
38 };
39 try {
40 this.mailSender.send(preparator);
41 }
42 catch (MailException ex) {
43 // simply log it and go on...
44 System.err.println(ex.getMessage());
45 }
46 }
47}
1import org.springframework.mail.MailException;
2import org.springframework.mail.MailSender;
3import org.springframework.mail.SimpleMailMessage;
4
5public class SimpleOrderManager implements OrderManager {
6
7 private MailSender mailSender;
8 private SimpleMailMessage templateMessage;
9
10 public void setMailSender(MailSender mailSender) {
11 this.mailSender = mailSender;
12 }
13
14 public void setTemplateMessage(SimpleMailMessage templateMessage) {
15 this.templateMessage = templateMessage;
16 }
17
18 public void placeOrder(Order order) {
19
20 // Do the business calculations...
21
22 // Call the collaborators to persist the order...
23
24 // Create a thread safe "copy" of the template message and customize it
25 SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
26 msg.setTo(order.getCustomer().getEmailAddress());
27 msg.setText(
28 "Dear " + order.getCustomer().getFirstName()
29 + order.getCustomer().getLastName()
30 + ", thank you for placing order. Your order number is "
31 + order.getOrderNumber());
32 try{
33 this.mailSender.send(msg);
34 }
35 catch(MailException ex) {
36 // simply log it and go on...
37 System.err.println(ex.getMessage());
38 }
39 }
40}
1<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
2 <property name="host" value="mail.mycompany.com"/>
3</bean>
4
5<!-- this is a template message that we can pre-load with default state -->
6<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
7 <property name="from" value="customerservice@mycompany.com"/>
8 <property name="subject" value="Your order"/>
9</bean>
10
11<bean id="orderManager" class="com.mycompany.businessapp.support.SimpleOrderManager">
12 <property name="mailSender" ref="mailSender"/>
13 <property name="templateMessage" ref="templateMessage"/>
14</bean>