How to use mail api in spring framework
Posted By : Gourav Kumar | 15-Apr-2020
In this blog, we will learn how to use mail API in spring framework. However, before we start, we need to know some important points.
i) Add some important dependencies given below:
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring-version}</version>
</dependency>
Here javax.activation and javax.mail dependencies are use for core java mail API but for spring we need to spring-context.support dependency which is provide org.springframework.mail root package for spring mail service and this package is contain following important interfaces and classes to implement mail service in spring framework.
Interfaces in org.springframework.mail packages:
i) MailMessage: This interface is a root interface of the mail message types and we have two mail message types here simple and mime mail message so it is contain two implementation class based on these two mail message type.
ii) MailSender: This interface is also a root interface and it is provide the functionality to send simple mail message.
iii) JavaMailSender: This interface is the child interface of MailSender interface and it is also provide the functionality to send mail message but it is supported both mail type simple and mime mail message.
iv) MailMessagePreparator: This is also an interface but it is a callback interface which is provide the facility to prepare a mime mail message.
Classes in org.springframework.mail:
i) SimpleMailMessage: This is an implementation class of MailMessage interface and it is used to create a simple mail message.
ii) MimeMailMessage: This is also an implementation class of MailMessage interface and it is used to create a mime mail message.
iii) JavaMailSenderImpl: This is an implementation class of JavaMailSender interface which is actually used to send a mail message.
iv) MimeMessageHelper: This class is used to prepare a mime mail message with an attachment.
ii) Now JavaMailSenderImpl bean definition set into applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.mail.service"/>
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"></property>
<property name="port" value="587"></property>
<property name="username" value="[email protected]"></property>
<property name="password" value="abc123"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
</beans>
iii) create MailService class:
Now we need to create a class MailService which is provide two method one for simple mail and another for mime mail message only we need to provide some basic information such as from, to, subject, body and attachment in case of mime mail message.
package com.mail.service;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class MailService
{
@Autowired
private JavaMailSender mailSender;
public boolean simpleMailPreparetor(final String from, final String to, final String subject, final String message)
{
try {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(from);
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(message);
mailSender.send(simpleMailMessage);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean mimeMailPreparator(final String from, final String to, final String subject, final String message,
final File file)
{
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(message);
mimeMessageHelper.addAttachment(file.getName(), file);
return true;
} catch (MessagingException e) {
e.printStackTrace();
}
return false;
}
}
iv) create Test class:
package com.mail.test;
import java.io.File;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mail.service.MailService;
public class Test
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext");
MailService mailService = context.getBean("mailService", MailService.class);
//Simple Mail
mailService.simpleMailPreparetor("[email protected]", "[email protected]", "This is a demo mail", "hello abc");
File attachment = new File("D:/demo.jpg");
//Mime mail
mailService.mimeMailPreparator("[email protected]", "[email protected]", "This is a demo mail", "hello abc see attachment", attachment);
}
}
Conclusion: I hope you will very well know how can we create a simple mail message or Mime mail message and send it through spring mail api.
Happy Coding
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Gourav Kumar
Gourav is a bright Web App Developer and has good knowledge of Core java, Spring and Hibernate and his hobbies listen and sing songs.