Quick Expense Manger. Your free expense manager. Lots of features. The application is also ad free.

Email Service with Spring Boot

Posted on Nov. 11, 2017
email-service-with-spring-boot

In this article I will explain how to write a Email Service using Spring Boot. I will show you how to use Rabbit MQ to receive the message to be sent using the email service.



Spring boot comes with lot of helpful stuff to make our life easier. We need to include the below dependency in our pom file.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

I don't need to explain much. The code is very self explanatory. However I will explain the Rabbit MQ stuff. We need to include the below dependency for Rabbit MQ

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>



Why we need a Message Queue ?

If we are writing something across micro services, we should use a message queue. Imagine you are calling the REST end point of your Email Service to send a email to your users but the Email Service is down at that point. Your message will simply get lost.



However if you use a message queue, the message will be persisted in the Message Queue and will be sent when the Email Service is UP again.



Here is how we set up an Input Channel to receive messages sent from other services.

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;

public interface EmailChannel {
@Input
SubscribableChannel emailInput();
}

We can set the Message end points to process the messages that we receive in our input channel as below.

@MessageEndpoint
public class NewEmailProcessor {

private EmailService emailService;

@Autowired
public NewEmailProcessor(EmailService emailService){
this.emailService = emailService;
}

@ServiceActivator(inputChannel = "emailInput")
public void onNewEmailRequest(Message<EmailDetails> emailDetails) throws Exception{
emailService.processEmailRequest(emailDetails.getPayload());
}
}

We can set our EmailDetails and EmailService as below.

@Configuration
public class EmailDetails {

private String emailTo;
private String emailSubject;
private String emailCC;
private String emailBody;

public EmailDetails(){

}

public EmailDetails(String emailTo, String emailSubject, String emailCC, String emailBody) {
this.emailTo = emailTo;
this.emailSubject = emailSubject;
this.emailCC = emailCC;
this.emailBody = emailBody;
}

public String getEmailTo() {
return emailTo;
}

public void setEmailTo(String emailTo) {
this.emailTo = emailTo;
}

public String getEmailSubject() {
return emailSubject;
}

public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}

public String getEmailCC() {
return emailCC;
}

public void setEmailCC(String emailCC) {
this.emailCC = emailCC;
}

public String getEmailBody() {
return emailBody;
}

public void setEmailBody(String emailBody) {
this.emailBody = emailBody;
}
}



@Service
public class EmailService {

private JavaMailSender sender;

@Autowired
public EmailService(JavaMailSender sender){
this.sender = sender;
}

public void processEmailRequest(EmailDetails emailDetails) throws Exception{
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo(emailDetails.getEmailTo());
helper.setText(emailDetails.getEmailBody());
helper.setSubject(emailDetails.getEmailSubject());

if(emailDetails.getEmailCC()!= null){
helper.setCc(emailDetails.getEmailCC());
}

sender.send(message);
}

}

The JavaMailSender is automatically injected by Spring Boot.

Now the most important thing which is the Configurations that we need to set for our Email Service to work properly. I am assuming that we will be using Gmail mail server to send the emails.



spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=yourEmailId
spring.mail.password=yourPasswordForYourEmailId
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
#Rabbit MQ configuration
spring.cloud.stream.bindings.emailInput.destination = mails
spring.cloud.stream.bindings.emailInput.group = mails-group
spring.cloud.stream.bindings.emailInput.durableSubscription = true
spring.cloud.stream.bindings.emailInput.contentType =
application/x-java-object;type=your.package.name.EmailDetails

Take a closer look at the Rabbit MQ configurations. The emailInput used in the line spring.cloud.stream.bindings.emailInput.destination = mails needs to exactly match the input channel name.

The destination value 'mails' should also match the output channel's destination. The output channel is used in some other service from where you are sending the EmailDetails object to be received in this side.


The 'mails-group' means that if you are running multiple instances of this email service, and if all those instances are using this same value for 'spring.cloud.stream.bindings.emailInput.group', then the message will be sent to only one instance to process.


That's how we can write a complete Email Service to send email messages in Spring Boot.

Sharing is Caring!

Quick Expense Manger. Your free expense manager. Lots of features. The application is also ad free.

GET FREE UPDATES


RECOMMENDED POSTS FOR YOU


profile image

Kaushik Baruah


ABOUT

My name is Kaushik Baruah and I am the chief blogger on this Blog and here I like to share my experience as software engineer and research engineer with my online readers. I will try to focus on career planning, latest emerging technologies and tutorials on various computer science subjects. You can follow me on Twitter, Facebook and Google+

GET FREE UPDATES

POPULAR POSTS

Copyright © 2016
About Us

My name is Kaushik Baruah and I am the chief blogger on this Blog and here I like to share my experience as software engineer and research engineer with my online readers. I will try to focus on career planning, latest emerging technologies and tutorials on various computer science subjects.

Get Free Updates