Java: Sending mails with Spring
In this blog we will take a look at creating a mailer util with Spring and the Spring MailSender class. First of all lets take a look at the part which has to be inserted into the application context xml file.
Configuring the Spring mail bean in the application-context
<!-- Spring Mail Sender config -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp-server.com" />
<property name="port" value="587" />
<property name="username" value="username" />
<property name="password" value="password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<!-- Here we initialize a utility which will use our bean.
This way it is possible to autowire/inject the mailer everywhere
it is needed.
-->
<bean id="mailUtility" class="me.kamwo.mail.utility.MailUtility" />
In this part we have done two different things. First we are telling the application that there is a Bean called mailSender and that it is of the type org.springframework.mail.javamail.JavaMailSenderImpl
. This class contains a implementation for the Java Mail API. The following part configures the “mail”-bean. It tells the application where to connect to and how to authenticate with the smtp-servers of the email provider.
The last part above is the definition of a new bean, called the mailUtility.
<bean id="mailUtility" class="me.kamwo.mail.utility.MailUtility" />
It initializes the MailUtility class, which uses the mailSender-Bean (first bean) and is nothing more than a wrapper around it. Declaring it as a Bean makes sending a mail very easy: You just need to autowire/inject it into your class and send the mail.
MailUtility: Wrapper around the Spring MailSender
Below you will find a quite simple wrapper around the MailSender class for sending plain text messages. It just depends on your use-cases ;-)
/*
* MailUtility.java
*
*/
package me.kamwo.mail.utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
/**
*
* @author kwozniak
*/
public class MailUtility {
@Autowired
private MailSender mailSender;
private String fromAddress = "admin@example.com";
private String toAddress = "someoneElse@example.com";
private String subject = "Default mailer subject";
private String message = "Hi, your order is ready :-)";
/**
* Sends an email.
*
* @param from Senders email address
* @param to Receivers email address
* @param subject Subject of the email
* @param msg msg of the email.
*/
public void sendMail(String from, String to, String subject, String msg) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(from);
mailMessage.setTo(to);
mailMessage.setSubject(subject);
mailMessage.setText(msg);
mailSender.send(mailMessage);
}
/**
* Sends an email. The senders address will be the default one.
*
* @param to Receivers email address
* @param subject Subject of the email
* @param msg msg of the email.
*/
public void sendMail(String to, String subject, String msg) {
sendMail(fromAddress, to, subject, msg);
}
/**
* Sends an email. The senders and recipient address will be the default ones.
*
* @param subject Subject of the email
* @param msg msg of the email.
*/
public void sendMail(String subject, String msg) {
sendMail(fromAddress, toAddress, subject, msg);
}
/**
* Sends an email. The senders and recipient address and also the default subject,
* will be the default one.
*
* @param msg msg of the email.
*/
public void sendMail(String msg) {
sendMail(fromAddress, toAddress, subject, msg);
}
}
Autowiring the mailUtility
Wherever you would like to use the mail utility, you can just inject it in any class you want. See the code fragment below.
@Inject
private MailUtility mailUtility;