Reputation: 3590
I am using spring boot 2.0.7 Release
and spring-boot-starter-mail-2.0.7.Release.
I am autowiring javaMailsender
inside the class working ok on windows while trying to deploy on Unix
getting belwo issue
APPLICATION FAILED TO START
***************************
Description:
Field javaMailSender in com.fti.di.capstock.tran.pub.email.SendEmail required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;
import com.fti.di.capstock.tran.pub.constants.ApplicationFlowConstants;
import com.fti.di.integration.constants.IntegrationConstants;
import com.fti.di.integration.util.StringUtil;
@Component("sendEmail")
public class SendEmail {
@Autowired
private JavaMailSender javaMailSender;
@Autowired
Environment env;
@ServiceActivator
Upvotes: 12
Views: 45822
Reputation: 21
This message is a Spring Boot application startup error, and it occurs when the application tries to create an EmailService object but cannot find the JavaMailSender object it needs.
The error indicates that the program cannot find a bean of type 'org.springframework.mail.javamail.javaMailSender' in the configuration of the Spring Boot application. To solve this problem, you must define the bean that the application needs in the configuration file of Spring Boot and make sure that it is accessible.
The following code can be added in the configuration file of the Spring Boot application to define a Bean of type 'org.springframework.mail.javamail.JavaMailSender':
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EmailConfig {
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
// set mailSender properties (host, port, username, password, etc.)
return mailSender;
}
}
The appropriate JavaMailSender properties must be specified on the email accounts that have been used. Then the application must be restarted with the debug option enabled to view the report showing more details about the problem.
Upvotes: 0
Reputation: 611
I have this error and it was related to missing properties in application.yml in the test folder.
Upvotes: 0
Reputation: 716
This error occurred due to the missing following properties in the application.yml file.
mail:
host: localhost
port: 1025
username: hello
password: hello
properties:
mail:
smtp:
ssl:
trust: "*"
auth: true
starttls:
enable: true
connectiontimeout: 5000
timeout: 3000
writetimeout: 5000
Upvotes: 1
Reputation: 549
the best answer I ve found was to check if you have a type in your application.properties:
spring.mail.host
spring.mail.username
spring.mail.password
spring.mail.port
Check the response of gleidson cardoso da silva from Could not autowire org.springframework.mail.javamail.JavaMailSender
Upvotes: 1
Reputation: 641
you've to provide the mail configuration in application.properties
spring.mail.host=MAIL_SERVER_IP
spring.mail.port=MAIL_SERVER_PORT
spring.mail.userName=USER_NAME
spring.mail.password=THE_PASSWORD
and if authentication not enable in server then remove userName and password and add this
spring.mail.properties.mail.smtp.auth=false
spring.mail.properties.mail.smtp.starttls.enable=false
Upvotes: 12
Reputation: 3037
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
@Configuration
public class Config {
@Bean
public JavaMailSender javaMailSender() {
return new JavaMailSenderImpl();
}
}
You can also return an instance of JavaMailSenderImpl()
from your @Bean
above saving you the hustle of having to implement a bunch of methods when you try to return the actual JavaMailSender()
class.
Upvotes: 15
Reputation: 3590
In My project i was reading email properties file like hostname, port etc from spring config server (Spring cloud).
I was missing a dependency at client end. Once i added that Dependency.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
JavamailSender class able to read those properties and worked fine.
In spring boot we need not define JavaMailSender bean manually. spring boot does itself.
Upvotes: 1
Reputation: 7235
Declare a @Bean of the type JavaMailSender
in a Configuration class (This is useful when you want to inject a class which is not part of your Spring Context, like a class that belongs to a 3rd-party lib, which happens to be your case). For example:
@Configuration
public class MyConfig {
@Bean
public JavaMailSender javaMailSender() {
return new JavaMailSender();
}
}
Make sure that the you have set the right properties under application.properties
as well.
Also, take a look into this question, as I believe this is a duplicate (if it's not, I am sorry)
Upvotes: 7