How To Send Email With Spring 4 email With Example

How To Send Email With Spring 4 email With Example

In This Article I would like to explain How To Send Email Using Framework.The Spring Framework provides org.springframework.mail.javamail.JavaMailSenderImpl class to Send Email With Help of JavaMail API. Lets See Complete Example.

Project Structure:

Tools Used In this Article

Java dependencies Tools Operating System
Java 1.8Spring 4.3.2
mail-1.4
activation-1.1
commons-logging-1.2
Maven 4.0.0
Ecclipse Neon
Windows 7

Add Maven dependencies to 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.java2study</groupId>
<artifactId>Spring4EmailExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>

</dependencies>

</project>

Write Configuration class

EmailAppConfig.java


package com.java2study;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@Configuration
public class EmailAppConfig {
@Bean("javaMailSenderImpl")
public JavaMailSenderImpl javaMailSenderImpl() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
mailSender.setUsername("your gmail Id");
mailSender.setPassword("your gmail password");
Properties prop = mailSender.getJavaMailProperties();
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.debug", "true");
return mailSender;
}
}

create The Test class and Run The code

TestEMailApp.java


package com.java2study;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class TestEMailApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(EmailAppConfig.class);
context.refresh();
JavaMailSenderImpl mailSender = (JavaMailSenderImpl)context.getBean("javaMailSenderImpl");
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper mail = new MimeMessageHelper(mimeMessage);
try {
mail.setFrom("SenderGmail@gmail.com");
mail.setTo("ReceiverGmail@gmail.com");
mail.setSubject("Sending Test e-mail");
mail.setText("Hello! Welcome To Java2study.com");
mailSender.send(mimeMessage);
} catch (MessagingException e) {

e.printStackTrace();
}

}
}

output

Console


DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 smtp.gmail.com ESMTP 6sm7973358pfo.132 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
EHLO Java2study-PC
250-smtp.gmail.com at your service, [103.228.223.46]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO Java2study-PC
250-smtp.gmail.com at your service, [103.228.223.46]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
YmFsYWphdmExNkBnbWFpbC5jb20=
334 UGFzc3dvcmQ6
bXVtbXkwNTE2
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:*****@gmail.com>
250 2.1.0 OK 6sm7973358pfo.132 - gsmtp
RCPT TO:<****@gmail.com>
250 2.1.5 OK 6sm7973358pfo.132 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP: ******@gmail.com
DATA
354 Go ahead 6sm7973358pfo.132 - gsmtp
Date: Thu, 18 May 2017 12:37:36 +0530 (IST)
From: *****@gmail.com
To: *******@gmail.com
Message-ID: <2012744708.01495091256565.JavaMail.Java2study@Java2study-PC>
Subject: Sending Test e-mail
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello! Welcome To Java2study.com
.
250 2.0.0 OK 1495091257 6sm7973358pfo.132 - gsmtp
QUIT
221 2.0.0 closing connection 6sm7973358pfo.132 - gsmtp

Here We need to make some changes gmail account other wise we will ge exception.tha is

log in to your gmail account and click below url

https://myaccount.google.com/lesssecureapps

next you will get screen like below

we need on the blue button in above image

-->