Baeldung

Java, Spring and Web Development tutorials

 

How to Resolve “Could Not Autowire org.springframework.mail.javamail.JavaMailSender”
2025-06-13 14:44 UTC by Martin Blažević

1. Introduction

In this tutorial, we’ll explore how to solve a common error we may encounter when implementing email functionality using Spring Boot.

We’ll focus on understanding the “Could not autowire org.springframework.mail.javamail.JavaMailSender” issue, why it occurs, and how to fix it.

2. Understanding the Error

Let’s start by explaining what this error means. JavaMailSender is an interface provided by Spring to abstract the email-sending process. It extends MailSender, another interface that offers basic functionality for sending simple text emails. Specifically, JavaMailSender supports more advanced email features like MIME messages, attachments, and HTML content.

Spring’s dependency injection mechanism automatically wires beans where needed. When it encounters the @Autowired annotation or, preferably, constructor injection, it searches the application context for a matching bean:

@Service
public class EmailService {
    private final JavaMailSender javaMailSender;
    public EmailService(final JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }
}

When we inject the JavaMailSender bean that Spring can’t find, and try to run the application, it throws an error:

Field javaMailSender in com.baeldung.email.EmailService required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.

Or:

Parameter 0 of constructor in com.baeldung.email.EmailService required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.

Next, we’ll examine why Spring Boot might fail to create or inject the JavaMailSender bean.

3. Potential Causes

Spring Boot makes email integration easy through auto-configuration but also supports manual bean definitions when customization is required. In both approaches, the application must meet certain conditions to make the JavaMailSender bean available in the application context.

3.1. Auto-Configuration Issues

To begin with, the spring-boot-starter-mail dependency provides an implementation of the JavaMailSender interface. If the project doesn’t include this dependency, Spring won’t even attempt to configure the bean.

On the other hand, if the project includes the correct dependency, but the required mail properties configuration is incomplete or incorrect, Spring Boot will skip creating the default JavaMailSender bean, resulting in an auto-wiring error.

Finally, if the application explicitly turns off auto-configuration for mail support, Spring will not attempt to create the bean.

3.2. Unregistered Manual Configuration

Alternatively, if we want to bypass the auto-configuration, we can manually define a JavaMailSender bean in a class marked with the @Configuration annotation. This approach is practical when dealing with multiple mail senders, advanced customization, or dynamic mail server settings that can’t be configured through standard Spring Boot properties.

In such cases, an incorrect package structure or component scanning misconfiguration may cause an autowiring error. Furthermore, Spring Boot’s auto-configuration is applied automatically unless we explicitly define a custom JavaMailSender bean.

4. Possible Solutions

Before we analyze the solutions, let’s set up the project and add the spring-boot-starter-mail dependency:

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

Next, let’s define a simple EmailService class. We’ll use this class in both auto-configuration and manual configuration examples:

@Service
public class EmailService {
    private static final String NOREPLY_ADDRESS = "noreply@baeldung.com";
    private final JavaMailSender javaMailSender;
    public EmailService(final JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }
    public void sendSimpleEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(NOREPLY_ADDRESS);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        javaMailSender.send(message);
    }
}

Finally, when implementing email sending, we have numerous options to choose from. Among these options, we can configure a Gmail SMTP server or run MailHog locally to test the functionality.

4.1. Enabling Auto-Configuration

Spring Boot attempts to configure JavaMailSender when the starter dependency is present. However, we also must define the required properties correctly. Specifically, spring.mail.host is the only mandatory property from the spring.mail property group that needs to be defined. Let’s set our properties to point to a local MailHog instance:

spring.mail.host=localhost
spring.mail.port=1025
spring.mail.username=
spring.mail.password=

With MailHog properties set, we can send a simple email with a basic application:

@SpringBootApplication(scanBasePackages = { "com.baeldung.email.service" })
public class EmailSenderApplication implements CommandLineRunner {
    private final EmailService emailService;
    public EmailSenderApplication(EmailService emailService) {
        this.emailService = emailService;
    }
    public static void main(String[] args) {
        SpringApplication.run(EmailSenderApplication.class, args);
    }
    @Override
    public void run(String... args) {
        emailService.sendSimpleEmail(
          "recipient@baeldung.com",
          "Test Subject",
          "Testing the Spring Boot Email!"
        );
    }
}

Finally, for this to work, we need to verify that the application doesn’t explicitly disable auto-configuration. For example, the following configuration would prevent Spring Boot from creating the JavaMailSender bean:

@SpringBootApplication(exclude = MailSenderAutoConfiguration.class)
public class EmailSenderApplication implements CommandLineRunner { ... }

Without such exclusions, Spring Boot can automatically configure and register the JavaMailSender bean.

4.2. Providing a Manual Bean Definition

Alternatively, if we manually define a JavaMailSender bean, we must ensure our bean is added to the Spring context correctly.

By default, Spring Boot scans for components starting from the package that contains the main application class and includes all of its sub-packages. Any classes annotated with @Service, @Component, @Configuration, or other Spring stereotype annotations will be scanned.

Let’s manually create our JavaMailSender bean to point at MailHog again:

@Configuration
public class EmailConfiguration {
    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("localhost");
        mailSender.setPort(1025);
        return mailSender;
    }
}

If our class is outside the base package of our application, Spring won’t detect it unless we provide additional configuration:

@SpringBootApplication(
  scanBasePackages = { "com.baeldung.email.config", "com.baeldung.email.service" }
)
public class EmailSenderApplication implements CommandLineRunner { ... }

By specifying the correct packages in scanBasePackages, we ensure that Spring detects and registers all required components.

5. Conclusion

In this article, we explored a common error that occurs when implementing email functionality with Spring Boot. We demonstrated how to avoid it using both auto-configuration and manual configuration, and we highlighted key considerations for each approach. With the correct dependencies and required properties in place, along with proper attention to package structure, we can easily integrate email functionality into any Spring Boot project.

As always, the complete source code is available over on GitHub.

The post How to Resolve “Could Not Autowire org.springframework.mail.javamail.JavaMailSender” first appeared on Baeldung.
       

 

Content mobilized by FeedBlitz RSS Services, the premium FeedBurner alternative.