Implement Mail Service Utility class in SpringBoot using SimpleMailMessage API

Posted by Somesh Shinde On Thursday, 18 May 2017 0 comments
Implement Mail Service Utility class in SpringBoot using SimpleMailMessage API



1. Create one EmailService Utility class ans paste this code.
2. Call this sendMail() method with particular parameters.


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
@Service
public class EmailService {

    private JavaMailSenderImpl javaMailSender;

    @Autowired
    public EmailService(JavaMailSenderImpl javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    public void sendMail(String toEmail, String subject, String message,String Otp) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(toEmail);
        mailMessage.setSubject(subject);
        mailMessage.setSubject(message);
        mailMessage.setText(Otp);
        mailMessage.setFrom("mailid@gmail.com");
        javaMailSender.send(mailMessage);
    }

}

0 comments:

Post a Comment