【问题标题】:Sending Email using VSCode (spring-boot-starter-email)使用 VSCode (spring-boot-starter-email) 发送电子邮件
【发布时间】: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


【解决方案1】:

由于我们不知道包结构,因此很难判断@ComponentScan 内的 basePackages 中应该有什么

首先,请将你的Application类在包结构中上移一层,使其默认读取其下的所有包,并在组件扫描中删除basePackages。所以,应该只是@ComponentScan

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

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

更新: 由于用户稍后更新了问题,因此我发布了对他有用的解决方案。

他将班级向上移动了一级并删除了 basePackages,这对他有用。正如我回答的第一部分所述。

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

【讨论】:

  • 请查看我更新的问题。我发布了我的代码结构,是的,我确实有服务注释。 @Sudoss
  • 所以我回答的第一部分应该适合你。请将应用程序类移出email 文件夹并从组件扫描中删除basePackages
  • 好的!让我试试,会告诉你的。 @Sudoss
  • 我将我的课程从电子邮件文件夹中移出并删除了 basePackages,我得到了一个 ClassCastException,我在我的问题中已经提到过。你能帮我么?要查看代码,请点击此链接technicalkeeda.com/spring-boot-tutorials/…@Sudoss
  • 这是一个单独的错误。请为此提出一个新问题并接受此答案,因为它已经解决了您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
相关资源
最近更新 更多