Spring Mail Sender

In this article, lets discuss about Sending email using Spring framework support.

Sending an email using Spring framework is made simple using org.springframework.mail.javamail.JavaMailSender and org.springframework.mail.javamail.MimeMessageHelper beans in Spring.

Consider the SpringMailSender class below.

SpringMailSender Class

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package in.techdive.spring.mail;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.mail.MailAuthenticationException;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class SpringMailSender
{
        private JavaMailSender  mailSender;
        private MimeMessage        message;
        private String         fromAddress;
        private String         toAddress;

        public SpringMailSender()
        {
        }

        public void sendMail(String messageBody, String subject)
        {
                try
                {
                        message = mailSender.createMimeMessage();
                        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
                        helper.setFrom(fromAddress);
                        helper.setSubject(subject);
                        helper.setTo(toAddress);
                        helper.setText(messageBody);
                }
                catch (MessagingException me)
                {
                        me.printStackTrace();
                }

                try
                {
                        mailSender.send(message);
                }
                catch (MailAuthenticationException ex)
                {
                        ex.printStackTrace();
                }
                catch (MailException me)
                {
                        me.printStackTrace();
                }
        }

        public JavaMailSender getMailSender()
        {
                return mailSender;
        }

        public void setMailSender(JavaMailSender mailSender)
        {
                this.mailSender = mailSender;
        }

        public String getFromAddress()
        {
                return fromAddress;
        }

        public void setFromAddress(String fromAddress)
        {
                this.fromAddress = fromAddress;
        }

        public String getToAddress()
        {
                return toAddress;
        }

        public void setToAddress(String toAddress)
        {
                this.toAddress = toAddress;
        }

        public static void main(String[] args)
        {
                ApplicationContext ctx = new FileSystemXmlApplicationContext(
                        new String[] { "classpath*:Spr-Example.xml" });

                SpringMailSender sprMailSender = (SpringMailSender) ctx.getBean("springMailSender");
                String messageBody = "";
                String subject = "";
                sprMailSender.sendMail(messageBody, subject);
        }
}

Have a look at the sendMail(..) function. It takes messageBody and subject as input parameters.

It creates a MimeMessage and then sets it to springs MimeMessageHelper bean. In this bean we need to set subject, fromAddress, toAddress, messageBody etc., The org.springframework.mail.javamail.JavaMailSender bean is injected in to the SpringMailSender class. It mainly contains the Smtp Port, Smtp Host Name, User Name and Password which are set in the application context.

Now look in to the application context for the beans which are injected.

Mail-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.techdive.in" />
<property name="username" value="" />
<property name="password" value="" />
</bean>
<bean id="springMailSender" class="SpringMailSender">
<property name="mailSender" ref="mailSender" />
<property name="fromAddress" value="admin_sender@techdive.in" />
<property name="toAddress" value="admin_receiver@techdive.in" />
</bean>
</beans>

In this way, we can use Spring framework to send emails.

Technology: 

Search