如果spring.mail.host不在application.properties中,则JavaMailSenderImpl自动连线错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如果spring.mail.host不在application.properties中,则JavaMailSenderImpl自动连线错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2949字,纯文字阅读大概需要5分钟。
内容图文
![如果spring.mail.host不在application.properties中,则JavaMailSenderImpl自动连线错误](/upload/InfoBanner/zyjiaocheng/683/16f3cf94ed574878950437c4620eef48.jpg)
我在使用JavaMailSenderImpl在Spring Boot应用程序中发送电子邮件时遇到了一些“问题”.
我正在尝试动态设置所有属性(我希望以后再从数据库中读取它们),但是由于我不知道的原因,自动装配JavaMailSenderImpl仅在application.properties中存在“ spring.mail.host”时才有效.
我设置的值无关紧要(它可以为空,也没关系,因为稍后再设置正确的值),但是该属性必须存在,否则自动装配将失败.
这是我的控制器:
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MailController {
@Autowired
private JavaMailSenderImpl ms;
@RequestMapping("/mail")
public String send(Model model){
SimpleMailMessage message;
String fromEmail="sdfsdf98435sadf@gmail.com";
String toEmail ="xxxxxxx";
Properties mailProperties = new Properties();
mailProperties.put("mail.smtp.starttls.enable", true);
mailProperties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
ms.setHost("smtp.gmail.com");
ms.setPort(587);
ms.setUsername("xxxx");
ms.setPassword("yyyyy");
ms.setJavaMailProperties(mailProperties);
message = new SimpleMailMessage();
message.setSubject("Test email");
message.setFrom(fromEmail);
message.setTo(toEmail);
message.setText("Something something");
try{
ms.send(message);
}
catch(MailException ex){
System.err.println(ex.getMessage());
}
return "OK";
}
}
使用此application.properties可以正常工作(发送电子邮件):
#springboot-starter-mail properties
spring.mail.host=
但是,如果我删除该行,将抛出此异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
... 19 common frames omitted
我可以将空属性留在那里,但是感觉不对.
任何想法可能是什么原因?
解决方法:
自动连接接口JavaMailSender而不是实现.
@Autowired
private JavaMailSender mailSender;
内容总结
以上是互联网集市为您收集整理的如果spring.mail.host不在application.properties中,则JavaMailSenderImpl自动连线错误全部内容,希望文章能够帮你解决如果spring.mail.host不在application.properties中,则JavaMailSenderImpl自动连线错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。