Introduction
Here we will see an example on sending mail using Spring email and Gmail SMTP server. We have other examples on sending emails using Spring or Java API, where we have used my own SMTP server to send emails but here we will see how to send email using Gmail SMTP server.
Prerequisites
Eclipse Neon, Java 1.8, Gmail SMTP, Gradle 4.10.2, Spring 5.1.7
Gmail Security setup for sending email using Gmail SMTP
Implementation of Example
Go through the following steps to implement sending mail using Spring and Gmail SMTP.
Creating Project
Create a gradle based project in Eclipse, the name of the project is spring-email-gmail-smtp.
Updating Build Script
Update the default generated build script to include required dependencies.
buildscript {
ext {
springVersion = '5.1.7.RELEASE'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath("io.spring.gradle:dependency-management-plugin:1.0.7.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile("org.springframework:spring-core:${springVersion}")
compile("org.springframework:spring-context-support:${springVersion}")
compile('javax.mail:mail:1.4.7')
testCompile('junit:junit:4.12')
}
Creating Configuration Class
Create below Spring configuration class to configure your gmail username, password and gmail SMTP for sending email.
package com.roytuts.spring.mail.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@Configuration
@ComponentScan(basePackages = "com.roytuts.spring.mail")
public class SpringEmailConfig {
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("smtp.gmail.com");
javaMailSender.setPort(587);
javaMailSender.setUsername("<your gmail address>@gmail.com");
javaMailSender.setPassword("<your gmail password>");
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
javaMailSender.setJavaMailProperties(properties);
return javaMailSender;
}
}
Creating Service Class
The below service class sends email to the intended recipient with the email Subject and email Body.
package com.roytuts.spring.mail.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
@Autowired
private MailSender mailSender;
public void sendEmail(final String fromEmail, final String subject, final String message,
final String[] emailAddresses) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(fromEmail);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setTo(emailAddresses);
simpleMailMessage.setText(message);
try {
mailSender.send(simpleMailMessage);
System.out.println("Email sending complete.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Creating Junit Class
To test our application we need below class whether we are able to send email to the recipient or not.
package com.roytuts.spring.email;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.roytuts.spring.mail.config.SpringEmailConfig;
import com.roytuts.spring.mail.service.EmailService;
public class EmailServiceTest {
private EmailService emailService;
@Before
public void setUp() throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SpringEmailConfig.class);
context.refresh();
emailService = context.getBean(EmailService.class);
context.close();
}
@Test
public void testSendEmail() {
final String[] addresses = new String[] { "<recipient's gmail address>@gmail.com" };
emailService.sendEmail("<your gmail address>@gmail.com", "How are you ?", "Hello Dear, How are you ?", addresses);
}
}
Testing the Application
Run the above Junit class to check whether you are able to send email to the recipient.
Console Output
Email sending complete.
Source Code
Thanks for reading.