Steps of Spring Boot Email Service

Faiz Akram
1 min readNov 18, 2020

--

  1. First You need to create a Spring Boot Project
  2. Add the below mention dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

3. Update application.properties file (Gmail SMTP and Amazon SES SMTP)

# Gmail SMTP
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<<Enter Gmail ID>>
spring.mail.password=<<Enter Your Gmail Password>>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
# Amazon SES SMTP
#spring.mail.host=email-smtp.us-west-2.amazonaws.com
#spring.mail.username=username
#spring.mail.password=password
#spring.mail.properties.mail.transport.protocol=smtp
#spring.mail.properties.mail.smtp.port=25
#spring.mail.properties.mail.smtp.auth=true
#spring.mail.properties.mail.smtp.starttls.enable=true
#spring.mail.properties.mail.smtp.starttls.required=true

4. Add Email configuration Class

package com.email.config;import java.util.Properties;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
public class EmailConfig {

@Value("${spring.mail.templates.path}")
private String mailTemplatesPath;


@Value("${spring.mail.host}")
private String mailServerHost;
@Value("${spring.mail.port}")
private Integer mailServerPort;
@Value("${spring.mail.username}")
private String mailServerUsername;
@Value("${spring.mail.password}")
private String mailServerPassword;
@Value("${spring.mail.properties.mail.smtp.auth}")
private String mailServerAuth;
@Value("${spring.mail.properties.mail.smtp.starttls.enable}")
private String mailServerStartTls;
@Bean
public FreeMarkerConfigurer freemarkerClassLoaderConfig() {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), "/" + mailTemplatesPath);
configuration.setTemplateLoader(templateLoader);
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
freeMarkerConfigurer.setConfiguration(configuration);
return freeMarkerConfigurer;
}


@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(mailServerHost);
mailSender.setPort(mailServerPort);
mailSender.setUsername(mailServerUsername);
mailSender.setPassword(mailServerPassword);
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", mailServerAuth);
props.put("mail.smtp.starttls.enable", mailServerStartTls);
props.put("mail.debug", "true");
return mailSender;
}
}

--

--

Faiz Akram
Faiz Akram

Written by Faiz Akram

Technical Lead at Compunnel

No responses yet