Spring comes with a useful 'Org.springframework.mail.javamail.JavaMailSenderImpl' class to simplify the E-mail s Ending process via JavaMail API. Here's a Maven build project to use Spring's 'javamailsenderimpl' to send an email via Gmail SMTP server.
1. Project Dependency
Add the JavaMail and Spring ' s dependency.
File:pom.xml
<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/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.mkyong.common</groupId> <artifactid>springexample </artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name >SpringExample</name> <url>http://maven.apache.org</url> <repositories> < repository> <id>Java.Net</id> <url>http://download.java.net/maven/2/</url> </ repository> </repositories> <dependencies> <dependency> <groupId> Junit</groupid> <artifactId>junit</artifactId> <version>3.8.1</versio N> <scope>test</scope> </dependency> <!--Java Mail API--<dependency> <groupId>javax.mail</groupId> <artifactid>mail& Lt;/artifactid> <version>1.4.3</version> </dependency> <!--Spring Framework-- <dependency> <groupId>org.springframework</groupId> <artifactid>spring</artifact id> <version>2.5.6</version> </dependency> </dependencies></project>
2. Spring ' s Mail Sender
A Java class to send email with the Spring ' s MailSender interface.
File:MailMail.java
Package Com.mkyong.common; Import Org.springframework.mail.mailsender;import org.springframework.mail.SimpleMailMessage; public class Mailmail{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 Simplemailmessa GE (); Message.setfrom (from), Message.setto (To), message.setsubject (subject); Message.settext (msg); Mailsender.send ( message);}}
3. Bean configuration file
Configure the MailSender bean and specify the email details for the Gmail SMTP server.
Note
Gmail Configuration details–http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
File:spring-mail.xml
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <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= "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> <bean id= "Mailmail" class= "Com.mkyong.common.MailMail" >< Property Name= "MailSender" ref= "MailSender"/></bean> </beans>
4. Run it
Package Com.mkyong.common; Import Org.springframework.context.applicationcontext;import Org.springframework.context.support.ClassPathXmlApplicationContext; public class App {public static void Main (string[] args) { ApplicationContext context = new CLASSPATHXM Lapplicationcontext ("Spring-mail.xml"); mailmail mm = (mailmail) context.getbean ("Mailmail"); Mm.sendmail ("[Email protected]", "[email protected]", " Testing123", "testing only \ n \ nthe Hello Spring email Sender "); }}
Referenc from:http://www.mkyong.com/spring/spring-sending-e-mail-via-gmail-smtp-server-with-mailsender/
Spring–sending e-mail Via Gmail SMTP Server with Mailsender--reference