首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用VSCode发送电子邮件(Spring Starter Email )

使用VSCode发送电子邮件(Spring Starter Email )
EN

Stack Overflow用户
提问于 2020-07-21 12:02:38
回答 1查看 544关注 0票数 2

我正在实现如何使用spring引导发送电子邮件,我试图在visual studio代码中实现这一点。但是它给了我以下错误

我在pom.xml中为电子邮件配置添加了以下两个依赖项:

代码语言:javascript
运行
复制
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

我的主要引导类:

代码语言:javascript
运行
复制
   @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"})注释有关。

有人能帮我解决这个错误吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-21 12:40:46

由于我们不知道包的结构,所以很难知道在basePackages内部的@ComponentScan中应该有什么

首先,请将应用程序类移到包结构的一个级别上,以便默认情况下读取其下的所有包,并在组件扫描中删除basePackages。所以,应该是@ComponentScan

也就是说,如果您的所有类都在包com.test.mailer中,那么应用程序类文件应该在com.test

试试这个,让我们知道,我也希望您有@Service注释为@Service("mailService")

更新:,因为用户稍后更新了这个问题,我将发布对他有用的解决方案。

他把这个班提升到一级,去掉了basePackages,它对他起了作用。正如我在答覆的第一部分所述。

或者,他可以在相同的结构中将@ComponentScan(basePackages = {"email"})更改为@ComponentScan("java.risknucleus")

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63014126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档