Send email using spring from Gmail and Yahoo
Spring Framework gives very helpful utility library to send an email which is responsible for doing all lower level resource handling for the client.
Its main package is org.springframework.mail which is root package of spring framework mail support. The interface MailSender is the central interface to send an email. A value object which encapsulates properties of mail such as subject,to, from (many others) whose class name is the SimpleMailMessage. This package also contains the hierarchy of classes checked exceptions whose root exception class is MailException.
Please have below step by step instruction to send email using spring from Gmail and Yahoo account. To send email from Gmail account SMTP server configuration done through dependency injection bean and to send email from yahoo account everything is pre-configured through dependency injection bean only we use class SimpleMailMessage to send an email as you will see example below:
To test send email using spring from Gmail and Yahoo below are needed:
- Eclipse ( We are using eclipse Kepler. You could also download eclipse from eclipse.org/downloads)
- Maven 3.0.4
- Project source code is available for download in the bottom of the page
Step 1: Create maven dynamic web project in eclipse name: SpringMailExample (Please use this link if you are not sure how to create maven project in eclipse: Create maven project in eclipse)
Step 2: Below is final project structure after creating all configuration:
Step 3: Please create application-config.xml file inside SpringMailExample\src\main\resources\spring\ folder anc copy paste below code:
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Gmail SMTP server properties --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com"/> <property name="port" value="587"/> <property name="username" value="gmailusername"/> <property name="password" value="password"/> <property name="javaMailProperties"> <props> <prop key="mail.transport.protocol">smtp</prop> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.starttls.enable">true</prop> <prop key="mail.debug">true</prop> </props> </property> </bean> <!-- Yahoo SMTP server properties --> <bean id="yahooMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="port" value="587"/> <property name="host" value="smtp.mail.yahoo.com"/> <property name="username" value="yahoousername@yahoo.com"/> <property name="password" value="password"/> <property name="javaMailProperties"> <props> <prop key="mail.transport.protocol">smtp</prop> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.starttls.enable">true</prop> <prop key="mail.debug">true</prop> </props> </property> </bean> <!-- Our main class to send email --> <bean id="sendMail" class="com.javahonk.SendEmail"> <property name="mailSender" ref="mailSender" /> <property name="yahooMailSender" ref="yahooMailSender" /> </bean> <!-- Pre configured mail properties to send mail from yahoo --> <bean id="sendMailFromYahoo" class="org.springframework.mail.SimpleMailMessage"> <property name="subject" value="Send email from yahoo"/> <property name="to" value="javahonk@gmail.com"></property> <property name="from" value="javahonk@yahoo.com"></property> <property name="text" value="Mail from spring framwork."></property> </bean> </beans>
Step 4: Please copy paste below dependency in pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework.samples.service.service</groupId> <artifactId>SpringMaven</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> </dependencies> </project>
Step 5: Please create class name: SendMail.java indise com.javahonk package and copy paste below code:
package com.javahonk; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; public class SendEmail { private MailSender mailSender; private MailSender yahooMailSender; public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } public void setYahooMailSender(MailSender yahooMailSender) { this.yahooMailSender = yahooMailSender; } public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "spring\\application-config.xml"); SendEmail sendEmail=(SendEmail)applicationContext. getBean("sendMail"); String sender="javahonk@gmail.com"; String receiver="javahonk@gmail.com"; sendEmail.sendEMailToGmailAccount(sender,receiver, "Test email Java Honk","Mail from spring framwork."); System.out.println("success"); //Pre-configured mail properties to send email from yahoo sendEmail.sendEMailFromYahoo(applicationContext); } public void sendEMailToGmailAccount(String from, String to, String subject, String msg) { //creating message SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(msg); mailSender.send(message); } public void sendEMailFromYahoo(ApplicationContext applicationContext) { SimpleMailMessage simpleMailMessage=(SimpleMailMessage) applicationContext.getBean("sendMailFromYahoo"); SimpleMailMessage mailMessage = new SimpleMailMessage( simpleMailMessage); yahooMailSender.send(mailMessage); } }
Step 6: Now we are all set to test our code. Right click SendEmail.java class –> Run As –> Java Application and it will show you below output on console and check your email to verify if you got an email.
Note: You will see below two debug warning on console please ignore it:
- DEBUG: java.io.FileNotFoundException: C:\Program Files (x86)\Java\jdk1.7.0\jre\lib\javamail.providers (The system cannot find the file specified)
- DEBUG: java.io.FileNotFoundException: C:\Program Files (x86)\Java\jdk1.7.0\jre\lib\javamail.address.map (The system cannot find the file specified)
If you want to fix this please create file name: javamail.providers and javamail.address.map inside ..\Java\jdk1.7.0\jre\lib\ folder.
Thant’s it Send email using spring from Gmail and Yahoo