Java Mail API
- The Spring framework offers a variety of helpful interfaces and classes that facilitate the sending and receiving of emails.
The Spring framework provides interfaces and classes for Java mail support as follows:
- MailSender interface: It is the root interface. It provides basic functionality for sending simple emails.
- JavaMailSender interface: It is the sub-interface of MailSender. It supports MIME messages.
- JavaMailSenderImpl class: It provides the implementation of the JavaMailSender interface. It supports JavaMail MimeMessages and Spring SimpleMailMessages.
- SimpleMailMessage class: It is used to create a simple mail message including from, to, cc, subject and text messages.
- MimeMessagePreparator interface: It is the callback interface for preparing JavaMail MIME messages.
- MimeMessageHelper class: It is the helper class for creating a MIME message. It supports inline elements such as images, typical mail attachments and HTML text content.
Following files for sending email through Spring framework.
- MailMail.java
- applicationContext.xml
- Test.java
Example: Mail.class
package com.mail.quipoin;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class Mail {
private MailSender mailSender;
public void setMailSender(MailSender mailSender) {
this.mailSender=mailSender;
}
public void sendMail(String from,String to,String subject,String msg) {
SimpleMailMessage message=new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(msg);
mailSender.send(message);
}
}
Test.java
package com.mail.quipoin;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class Test {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory b=new XmlBeanFactory(r);
Mail m=(Mail) b.getBean("mailMail");
String sender="naveenrm36@gmail.com";//sender g-mail id
String receiver="naveenrm38@gmail.com";//Receiver g-mail id
m.sendMail(sender, receiver, "Offer letter", "Welcome to the quipo family");
System.out.println("Success!!");
}
}
applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="username" value="naveenrm36@gmail.com" />
<property name="password" value="Naveen@333" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.port">465</prop>
</props>
</property>
</bean>
<bean id="mailMail" class="com.mail.quipoin.Mail">
<property name="mailSender" ref="mailSender" />
</bean>
</beans>
Output:
Success!!