java - 使用 VSCode (spring-boot-starter-email) 发送电子邮件
问题描述
我正在实现如何使用 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"})
我上面使用的注释有关
谁能帮我解决这个错误?
解决方案
由于我们不知道包的结构,因此很难判断 basePackages 里面应该有什么@ComponentScan
首先,请将您的 Application 类在包结构中上移一级,使其默认读取其下的所有包,并在组件扫描中删除 basePackages。所以,应该只是@ComponentScan
也就是说,如果您的所有类都在包中,com.test.mailer
那么您的应用程序类文件应该在com.test
试试这个,让我们知道,我也希望你有@Service
注释@Service("mailService")
更新: 由于用户稍后更新了问题,我发布了对他有用的解决方案。
他将班级向上移动了一级并删除了 basePackages,这对他有用。正如我回答的第一部分所述。
或者,他本可以更改@ComponentScan(basePackages = {"email"})
为@ComponentScan("java.risknucleus")
相同的结构。