Spring Mail Integration

Why to use spring mail API

If you have used spring framework you must have observed that spring shields you from caring about the underlying technology details and configurations. Similarly in the case of Spring Mail API you do not need to manage the low level resources, Spring does it, and it provides number of utilities to ease the development.

What you need to know in order to use spring mail API

1 Basic knowledge of spring framework and its configurations
2 Dependency Injection and annotations

If you are not familiar with above two then you must give a drill through these and also try a spring demo, so that you have a basic idea about the configurations.

Infrastructure set-up

1 An SMTP server in range, you can also use gmail as your SMTP server
smtp.gmail.com

2 A port number on the SMTP server, by default it is 25 but in case of gmail it is 587
Note: The use of SMTP server, port depends on the infrastructure that you have

3 A username and password that would be used to authenticate the sender, you can use your own gmail account

Jar Files

mail. jar ( Java Mail ), activation.jar , spring-context-support.jar (assuming other spring related jars are available).

Configuring Spring

Paste the following entries in your spring bean configuration or application context file

<beanid=“mailSender” class=“org.springframework.mail.javamail.JavaMailSenderImpl”>

<property name=“username” value=“yourusername@gmail.com”></property>

<property name=“password” value=“yourpassword”></property>

<property name=“host” value=“smtp.gmail.com”></property>

<property name=“port” value=“587”></property>

<propertyname=“javaMailProperties”>

<props>

<prop key=“mail.smtp.starttls.enable”>true</prop>

<prop key=“mail.smtp.auth”>true</prop>

</props>

</property>

</bean>

The below entry is to instantiate the JavaMailSenderImpl that implements the MailSender Interface, the core interface that handles most of the functionality, further the JavaMailSender interface adds the functionality of Mime Messages (Attachments, Inline Images).

<bean id=”mailSender” class=”org.springframework.mail.javamail.JavaMailSenderImpl” >

With the below properties we are passing the username, password and SMTP server detail
Note: if your SMTP server does not require  authentication there is no need to provide username, password (this is the case mostly in production)

<property name=”username” value=”yourusername@gmail.com”></property>

<property name=”password” value=”yourpassword”></property>

 <property name=”host” value=”smtp.gmail.com”></property>

 <property name=”port” value=”587″></property>

As spring mail internally uses the java mail API, the below properties are what Spring requires to pass on to the java mail API.These properties are used to define the authentication process.

Notes:

1 If your SMTP server does not require any authentication then both these properties should be false

2 If your SMTP server does require authentication then both these entries must be true

e.g

<property name=”javaMailProperties”>

<props>

<prop key=”mail.smtp.starttls.enable”>true</prop>

<prop key=”mail.smtp.auth”>true</prop>

</props>

</property>

Getting things in action:

@Service

public class DemoMail {

@Autowired

JavaMailSenderImpl mailSender;

public void sendDemoMail() {

MimeMessage message =null;

MimeMessageHelper helper=null;

try

{

message = mailSender.createMimeMessage();

helper = new MimeMessageHelper(message);

helper.setTo(“test@host.com”);

helper.setSubject(“Test Subject”);

helper.setText(“Thank you for ordering”);

mailSender.send(message);

 

}catch(MessagingException me)

{

me.printStackTrace();

}

}

}

We injected the bean mailSender that we configured in context file to the service class that needs to send the mail
@Autowired
JavaMailSenderImpl mailSender;

Instantiating  the message helper object , the object can be used for sending inline resources, rich text and attachments. However you can use the message instance without helper if you just want to send the text.

message = mailSender.createMimeMessage();
helper = new MimeMessageHelper(message);

Rest of the action can be clearly understood by reviewing the above class. Refer the below link for more information .

http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/mail.html

Some standard practices

Never hard code the mail configuration details like server, username in code or context file (I have done this for the sake of simplicity). Read them from some property file.

2  Always use some template like velocity for managing  rich text in the mail.

 Try to send the mail in a separate java thread.

You can also try the demo application SpringMail

4 thoughts on “Spring Mail Integration

  1. Very nicely written article. One question :

    @Autowired
    JavaMailSenderImpl mailSender;

    Shouldn’t it be :
    @Autowired
    MailSender mailSender; //OR JavaMailSender mailSender;

    By the nature we should have interfaces for association not the implementation. Infact using the Impl, limits it’s use to be extended from JavaMailSenderImpl and not from MailSender or JavaMailSender.

    • According To CDI(Context Dependency Injection) concept, We can use interface for auto wiring. Suppose we have two implementation of interface then we can use @Qualifier to distinguish between both interface.

Leave a Reply

Your email address will not be published. Required fields are marked *


8 + = fifteen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>