【发布时间】:2020-07-21 21:12:00
【问题描述】:
我正在实现如何使用 spring boot 发送电子邮件 我正在尝试在 Visual Studio 代码中实现这一点。 但它给了我以下错误
我在 pom.xml 中添加了以下两个依赖项用于电子邮件配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
我的主要引导类:
@SpringBootApplication
@ComponentScan(basePackages = {
"email"
})
public class Application {
public static void main(String[] args) {
Mail mail = new Mail();
mail.setMailFrom("abc@gmail.com");
mail.setMailTo("xyz@gmail.com");
mail.setMailSubject("Hi");
mail.setMailContent("Hope you are doing well");
ApplicationContext ctx = SpringApplication.run(Application.class,
args);
MailService mailService = (MailService) ctx.getBean("mailService");
mailService.sendEmail(mail);
}
我认为我的错误与我上面使用的@ComponentScan(basePackages = {"email"}) 注释有关
谁能帮我解决这个错误?
【问题讨论】:
-
为什么你认为你需要@ComponentScan?如果你所有的 Spring-Beans 都在你的 Application 类的包之下,它应该在没有 ComponentScan 的情况下工作。你有一个名为“mailService”的类吗?
-
我在删除 ComponentScan 后运行了我的项目。它给了我一个不同的错误。请参阅我更新的问题。是的,我有一个名为 mailservice @TomStroemer 的类
标签: java spring-boot