如果application.properties中没有spring.mail.host,则JavaMailSenderImpl autowire错误
我在spring boot应用程序中使用JavaMailSenderImpl发送电子邮件时遇到了一个小问题 我正在尝试动态设置所有属性(我希望将来从数据库中读取它们),但是,由于我不知道的原因,只有在我的application.properties中存在“spring.mail.host”时,自动连接JavaMailSenderImpl才会起作用 我设置的值不重要(它可以是空的,因为我以后设置了正确的值),但是属性必须存在,否则自动连接将失败 这是我的控制器:如果application.properties中没有spring.mail.host,则JavaMailSenderImpl autowire错误,java,spring,spring-boot,jakarta-mail,autowired,Java,Spring,Spring Boot,Jakarta Mail,Autowired,我在spring boot应用程序中使用JavaMailSenderImpl发送电子邮件时遇到了一个小问题 我正在尝试动态设置所有属性(我希望将来从数据库中读取它们),但是,由于我不知道的原因,只有在我的application.properties中存在“spring.mail.host”时,自动连接JavaMailSenderImpl才会起作用 我设置的值不重要(它可以是空的,因为我以后设置了正确的值),但是属性必须存在,否则自动连接将失败 这是我的控制器: import java.util.
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";
}
}
此应用程序可以正常工作(发送电子邮件)。属性:
#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;
不要自动连接
JavaMailSenderImpl
。相反,创建一个方法,该方法创建并运行JavaMailSenderImpl
的实例,并返回它和您需要的所有参数。然后在需要的地方给你的邮件寄件人打电话。如果您自动连接mailsender,它将注入一个已经需要参数spring.mail.host
的实例。创建JavaMailSenderImpl
的触发器是spring.mail.host
属性的存在。不在那里和空着是不同的。另外,你正在做的事情是你一开始就不应该做的。如果希望从数据库中进行配置,那么就这样做,但不要在运行时重新配置bean。在启动时加载所有属性(创建一个Jdbc驱动的PropertySource实现),并让spring为您执行注入。为什么还要使用数据库?为什么不是spring cloud config或其他什么。@M.Deinum非常感谢您的解释,我不知道这是触发器。这是一个允许用户使用其电子邮件帐户发送邮件的应用程序。这些信息将保存在每个用户的数据库中,这就是我动态设置属性的原因。我不知道是否有更好的解决方案,我不是专家,所以非常感谢您的建议。我如何使用JavaMailSender动态设置主机、端口、用户名等?