DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Integrate Spring With Open AI
  • Secure Spring Boot Application With Keycloak
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • How to Interpret the Number of Spring ApplicationContexts in Integration Tests

Trending

  • Rebuilding Elasticsearch Under Load: Unsafe Watermarks and a Sampling Blind Spot
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Genkit Flows as senro Steps: Extremely Powerful AI Pipelines in Go

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Send Email Using Spring Boot (SMTP Integration)

Send Email Using Spring Boot (SMTP Integration)

This article explores Spring Boot integration with SMTP and how to send emails from your very own Spring Boot application.

By 
Akash Verma
·
May. 30, 23 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

This article explores how to send emails from your very own Spring Boot application.

Yes, you can have a dedicated REST API, which accepts the email sender's and receiver's email addresses, the subject, and the body — the classic attributes that make up a business email. This API can then be invoked at will from your front-end team by passing in the necessary parameters, and Voila! Your email is sent in a breeze.

Spring Boot provides a built-in dependency, which has all the required methods. This can be used to send the email to a valid email address. It's completely free of cost and very easy to integrate with in a classic Spring Boot application.

In this article, we'll look at how to send emails from a simple Spring Boot application.

Spring uses SimpleMailMessage and does it by integrating with spring-boot-starter-mail.

So, let's get started.

Step 1

Head over to start.spring.io and create a new Spring Boot project with just the below dependencies

  • Spring web
  • Java Mail sender

Click "generate project." This will create a zip file, extract it and open it in IntelliJ.

At this point, you have a base Spring Boot project. Just start the application by hitting the play button to ensure everything is up and running.

Checking application sanity

 Checking application sanity

We are good to go for integration with JMS!

Step 2: Configuring Our Email Server

Next, we need to provide the configuration for Spring Mail. Simply add the below properties for your email configuration to the application.properties file:

Java
 
# email configs

spring.mail.host=smtp.gmail.com

spring.mail.username=<your email id>

spring.mail.password=<your password>

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true


Step 3

With all the required config in place, we are ready to expose our rest endpoint/mail, which will be accepting incoming requests to send emails to a particular email address.

Let's add the below code for the controller layer to be able to hit our API, which will trigger the email.

Java
 
@RestController
public class EmailController {

    @Autowired
    private EmailService emailService;

    @PostMapping("/mail")
    public void sendEMail(@RequestBody EmailRequest emailRequest) {
        System.out.println("Going to Send email: " + emailRequest.toString());
        emailService.sendEmail(emailRequest);
    }
}


As we can see, the controller is expecting the request object named EmailRequest.

So, make a request body for the email request, which consists of the below attributes to identify the email:

Java
 
public class EmailRequest {

    // Class data members
    private String recipient;
    private String msgBody;
    private String subject;
    private String attachment;
    
    //generate getters 
}


Service Layer

Let's talk about the implementation class. It will contain an object of the JavaMailSender which has a method called send() which has the below signature:

void send(SimpleMailMessage simpleMessage) throws MailException;

The SimpleMailMessage class is from the package  "org.springframework.mail".

It has attributes like from, to, text, and subject. We set all these vital attributes from our incoming request. as below and will ultimately use the 'javaMailSender' to fire the send method.

Java
 
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void sendEmail(EmailRequest emailRequest) {

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("abc@xyz");
        mailMessage.setTo(emailRequest.getRecipient());
        mailMessage.setText(emailRequest.getMsgBody());
        mailMessage.setSubject(emailRequest.getSubject());
        javaMailSender.send(mailMessage);
    }
}


With this, we are done! Start the application and head over to Postman. We are using the default port for spring (which is 8080) hence call the API at localhost:8080/mail

Call the API from Postman as below:

Calling API via Postman at /mail

 Calling API via Postman at /mail

Conclusion

In this article, we learned a simple way to send emails from our spring boot application. It uses JMS JavaMailSender for sending emails using a simple REST API. 

API Spring Boot Integration

Opinions expressed by DZone contributors are their own.

Related

  • Integrate Spring With Open AI
  • Secure Spring Boot Application With Keycloak
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • How to Interpret the Number of Spring ApplicationContexts in Integration Tests

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook