当自动装配javamailsender时出现以下错误:
字段javaMailSender中
com.frs.services.MailService.impl.MailServiceImpl需要一个Bean
键入“ org.springframework.mail.javamail.JavaMailSender”无法
被发现。
注入点具有以下注释:
@org.springframework.beans.factory.annotation.Autowired(required=true)
@Autowired
private JavaMailSender javaMailSender;
@Override
@Transactional
public void addMail(MailServiceDTO mailServiceDTO) throws MessagingException {
logger.debug(LogMarker.ENTRY, "Send Email with Subject Message to Email Addresses ");
Properties props = ((JavaMailSenderImpl) javaMailSender).getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
message.setFrom(mailServiceDTO.getEmailFrom());
message.setTo(mailServiceDTO.getEmailTo());
message.setSubject("Text");
javaMailSender.send(mimeMessage);
使用的依赖项:
<!-- Mail Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
最佳答案
您需要在配置文件中提供邮件属性
# configuration email
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=email
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
否则您将需要在配置文件中拥有自己的bean
@Bean
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setProtocol("SMTP");
javaMailSender.setHost("smtp.gmail.com");
javaMailSender.setPort(587);
return javaMailSender;
}