Usage - Java-Springs

Let's assume there is a business interface called OrderManager:

public interface OrderManager {
void placeOrder(Order order);

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
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class SimpleOrderManager implements OrderManager {
private MailSender mailSender;
private SimpleMailMessage templateMessage;
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
public void setTemplateMessage(SimpleMailMessage templateMessage) {
this.templateMessage = templateMessage;
public void placeOrder(Order order) {
// Do the business calculations...
// Call the collaborators to persist the order...
// Create a thread safe "copy" of the template message and customize it
SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
"Dear " + order.getCustomer().getFirstName()
+ order.getCustomer().getLastName()
+ ", thank you for placing order. Your order number is "
+ order.getOrderNumber());
catch(MailException ex) {
// simply log it and go on...

Find below the bean definitions for the above code:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value=""/>
<!-- this is a template message that we can pre-load with default state --
<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value=""/>
<property name="subject" value="Your order"/>
<bean id="orderManager" class="">
<property name="mailSender" ref="mailSender"/>
<property name="templateMessage" ref="templateMessage"/>

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Java-Springs Topics