How I ensured user authentication, by sending emails in Spring Boot

How to send email in Java Spring Boot

First of all, add the spring-starter-mail dependency.
This can also be added by modifying your project pom.xml file to include the following

<dependency>
<groupId>org.springframework.b…


This content originally appeared on DEV Community and was authored by walerick

How to send email in Java Spring Boot

First of all, add the spring-starter-mail dependency.
This can also be added by modifying your project pom.xml file to include the following

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

After you've successfully added the dependency, create a class object and access the the JavaMailSender Interface in your code by following these few steps:

  • Use the @Autowired annotation, to inject he JavaMailSender interface into the EmailService class.

  • Create a DemoMailMessage object.

  • Add email properties by using specific methods (setTo, setSubject, setText).

  • Call the send() method to send the email message.

Your code should be similar to this:

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

public class MyClass {
    @Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    public void sendEmail(String to, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);

        mailSender.send(message);
    }
}
}

Finally, you need to configure the SMTP server settings in your application.properties file as shown below

spring.mail.host=smtp.example.com
spring.mail.port=25
spring.mail.username=setusername
spring.mail.password=setpassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Then you're good to go.

Since you made it this far, There is an internship program, that I stumbled upon twitter where developers can learn more and network better. There is also an option to get Certificate if you subscribe to the premium package instead.


This content originally appeared on DEV Community and was authored by walerick


Print Share Comment Cite Upload Translate Updates
APA

walerick | Sciencx (2024-06-30T16:56:50+00:00) How I ensured user authentication, by sending emails in Spring Boot. Retrieved from https://www.scien.cx/2024/06/30/how-i-ensured-user-authentication-by-sending-emails-in-spring-boot/

MLA
" » How I ensured user authentication, by sending emails in Spring Boot." walerick | Sciencx - Sunday June 30, 2024, https://www.scien.cx/2024/06/30/how-i-ensured-user-authentication-by-sending-emails-in-spring-boot/
HARVARD
walerick | Sciencx Sunday June 30, 2024 » How I ensured user authentication, by sending emails in Spring Boot., viewed ,<https://www.scien.cx/2024/06/30/how-i-ensured-user-authentication-by-sending-emails-in-spring-boot/>
VANCOUVER
walerick | Sciencx - » How I ensured user authentication, by sending emails in Spring Boot. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/30/how-i-ensured-user-authentication-by-sending-emails-in-spring-boot/
CHICAGO
" » How I ensured user authentication, by sending emails in Spring Boot." walerick | Sciencx - Accessed . https://www.scien.cx/2024/06/30/how-i-ensured-user-authentication-by-sending-emails-in-spring-boot/
IEEE
" » How I ensured user authentication, by sending emails in Spring Boot." walerick | Sciencx [Online]. Available: https://www.scien.cx/2024/06/30/how-i-ensured-user-authentication-by-sending-emails-in-spring-boot/. [Accessed: ]
rf:citation
» How I ensured user authentication, by sending emails in Spring Boot | walerick | Sciencx | https://www.scien.cx/2024/06/30/how-i-ensured-user-authentication-by-sending-emails-in-spring-boot/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.