Tuesday, February 14, 2023

How to send Email in Java using Spring Framework? JavaMailSenderImpl Example Tutorial

Hello guys, if you want to send emails from your Java application and looking for a Java + Spring Framework tutorial to send emails then you have come to the right place. Earlier, I have shared the free spring framework online courses, and today, I am going to share how to send Emails in the Java application using Spring Framework and JavaMailSenderImpl class which makes it really easy to send emails from Java application.  Email is one of the essential function of any enterprise Java application. Your application will need Email functionality to send reminders, bills, payments, confirmations, passwords, alerts, and several other kinds of system notifications.

There is hardly any enterprise application in Java that doesn't send emails. It could be for a functional reason or non-functional requirements but you will find yourself writing code to send emails every now and then, but it's not straightforward in Java. 

You need to include Java Mail API and learn some key classes and how to configure them to send emails from Java Programs. You can see the whole process in my earlier article about how to send emails from Java Program, but the point is that it's not so easy. 

Fortunately, Spring's JavaMailSenderImpl makes sending emails from Java ridiculously easy. It's a production implementation of JavaMailSender interface and supports both SimpleMailMessage from Spring, and MimeMessages from JavaMail API.

The best thing about this class is that it allows you to define mail settings as bean properties as well as using a pre-configured JavaMail Session, which can be obtained from an application server's JNDI environment.

I am expecting that you already know the Spring framework, at least you are familiar with writing Spring configuration files and declaring Spring beans using XML or configuring them using Java and Annotation configuration method, but if you are not then I suggest you first go through a comprehensive Spring online courses to learn basics. 

This list contains the best and most up-to-date courses where you will learn everything about the Spring framework from scratch by one of the best Spring instructors on Udemy, John Thomson.  It also covers the latest Spring 5 features like Reactive Programming and WebFlux.




How to send Mail in Java using Spring Framework? Example

Here is your step by step guide to sending email from a Java application using Spring Framework and its utility classes like JavaMailSenderImpl another template class that takes away most of the pain associated with sending emails using Java mail API

Step 1: Configure JavaMailSenderImpl as Spring bean in ApplicationContext.xml

<bean id="mailSender" class="org.springframework.mail.javamail.
                                JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com"/>
    <property name="port" value="25"/>
    <property name="username" value="#emailid"/>       
    <property name="password" value="#password"/>

    <property name="javaMailProperties">
        <props>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtps.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.smtp.ssl.enable">false</prop>
            <prop key="mail.debug">true</prop>
        </props>
    </property>
</bean>




Step 2: Configure our test application with JavaMailSenderImpl reference.

<bean id="javaApp" class="com.javarevisited.demo.SpringEmailDemo">
    <property name="mailSender" ref="mailSender"/>
</bean>
How to send Email in Java using Spring Framework - JavaMailSenderImpl Example


Step 3: Create our Java application to send Email

package com.javarevisited.demo;

public class SpringEmailDemo {
    private JavaMailSender mailSender;
    //setter getters

    public MimeMessage getMailMessage() throws MessagingException {
        final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false);
        helper.setFrom("javarevisited@gmail.com");
        helper.setTo("abc@gmail.com");
        helper.setCc("def@gmail.com");
        helper.setBcc("fgh@gmail.com");
        helper.setSubject("How are you doing?");
        helper.setText("This is a test mail",true);
        return mimeMessage;
    }

    public void sendMail() throws MailException, MessagingException {
        mailSender.send(getMailMessage());
    }
}


That's all about how to send emails from Java using the Spring framework. This is a concise example, but Spring is mighty for sending all kinds of emails like email with an attachment, an email with HTML content, an email with images, and so on. 

If you want to learn more about those features, I suggest you join a comprehensive Spring framework course like I have recommended in my list of best Spring Framework courses and books, those will teach you everything about Spring Framework.



Other Java and Spring articles you may like
  • 5 Spring Boot Features Every Java Developer Should Know (features)
  • Spring Boot + ThyMyleaf project example (thymyleaf example)
  • The Java Developer RoadMap (roadmap)
  • How to test Spring boot application in Java? (spring boot testing example)
  • 10 Tools Java Developers use in their day-to-day life (tools)
  • Spring Boot Integration test example (integration test example)
  • 15 Spring Boot Interview Questions for Java Programmers (questions)
  • 10 Tips to become a better Java developer (tips)
  • How to post JSON data using Postman? (postman json example)
  • 3 Best Practices Java Programmers can learn from Spring (best practices)
  • 10 Spring Core Annotations Java Developers should learn (annotations)
  • 5 courses to learn Spring Boot and Spring Cloud( courses)
  • 3 ways to change Tomcat port in Spring Boot (tutorial)
  • Top 5 Free Courses to learn Spring and Spring Boot (courses)
  • 5 Course to Master Spring Boot online(courses)
  • 10 Things Java Developer should learn(goals)
  • 10 Spring MVC annotations Java developers should learn (annotations)
  • 7 Courses to learn Microservices for Java developers (courses)
  • 10 Advanced Spring Courses for Experienced Programmers (courses)

Thanks for reading this article so far. If you like this Java and Spring example to send emails, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If you want to learn Spring and looking for a free online course then you can also check out these free Spring Core and Spring MVC courses, it's completely free and all you need to do is create an account to enroll in this course.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.