javaspring-bootemailsmtp

Error trying to send an email with BootSpring Email


I'm trying to send an email using Spring Boot, this is the first time I'm trying to do this and I've followed a tutorial to try to do it, so far idk what's wrong, I think I've set everything up right, here are the codes, I'd love the help

aplication.properties:

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=#myemail#@gmail.com
spring.mail.password=#my app password#
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

EmailService class to i can send the emails

package maple.spring_servlet.service;

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

@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);
        
    }
}

The unit test i'm using to run the class and trying to send the email:

package maple.spring_servlet;

import maple.spring_servlet.service.EmailService;
import org.junit.jupiter.api.Test;
import org.springframework.mail.javamail.JavaMailSender;


public class EmailServiceTest {

    @Test
    public void testSendEmail() {

        EmailService emailService = new EmailService();

        String to = "#recipientemail#.com";
        String subject = "Test Subject";
        String body = "Test Body";

        emailService.sendEmail(to, subject, body);

    }
}

The error:

java.lang.NullPointerException: Cannot invoke "org.springframework.mail.javamail.JavaMailSender.send(org.springframework.mail.SimpleMailMessage)" because "this.mailSender" is null

    at maple.spring_servlet.service.EmailService.sendEmail(EmailService.java:23)
    at maple.spring_servlet.EmailServiceTest.testSendEmail(EmailServiceTest.java:19)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1597)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1597)

Solution

  • I fixed it, I didn't know that when you create a SpringBootTest you have to do it this way:

    @SpringBootTest
    class SpringServletApplicationTests {
    
        @Test
        void contextLoads() {
        }
    
    }
    

    Thanks to that, I was then able to add:

    @Autowired
    private EmailService emailService;
    

    So that emailService could automatically inject whatever dependencies it needed to work.

    In the end, my working test looked like this:

    package maple.spring_servlet;
    
    import maple.spring_servlet.service.EmailService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    public class EmailServiceTest {
    
        @Autowired
        private EmailService emailService;
    
        @Test
        public void testSendEmail() {
            emailService.sendEmail(#recipientemail#.com",
                    "Test Email",
                    "This is a test email sent from Spring Boot application.");
        }
    }
    

    Hope this helps someone else who runs into the same issue!