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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Integrate Spring With Open AI
  • Secure Spring Boot Application With Keycloak
  • How to Introduce a New API Quickly Using Micronaut
  • Failure Handling Mechanisms in Microservices and Their Importance

Trending

  • How to Create a Successful API Ecosystem
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • Code Reviews: Building an AI-Powered GitHub Integration
  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 user avatar
Akash Verma
·
May. 30, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
8.3K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • How to Introduce a New API Quickly Using Micronaut
  • Failure Handling Mechanisms in Microservices and Their Importance

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!