Sending Email using VSCode (spring-boot-starter-email)

Viewed 479

I am implementing how to send email using spring boot I am trying to implement this in visual studio code. But it gives me the following error

enter image description here

I added the following two dependencies in my pom.xml for email configuration:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

my main bootstrap class:

   @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);  

    
     }

I think my error is related to the @ComponentScan(basePackages = {"email"}) annotation that I have used above

Can anyone help me with the error?

1 Answers

Since we don't know the package structure it is difficult to tell what should be there in the basePackages inside @ComponentScan

Firstly, please move your Application class to one level up in the package structure, so that it reads all packages under it by default and remove the basePackages in component scan. So, it should be just @ComponentScan

That is, if all your classes are in package com.test.mailer then your Application class file should be in com.test

Try this and let us know, also I hope you have the @Service annotation as @Service("mailService")

Update: Since the user has updated the question later, I am posting the solution that worked for him.

He moved the class one level up and removed the basePackages and it worked for him. As stated in the first part of my answer.

Alternatively, he could have changed @ComponentScan(basePackages = {"email"}) to @ComponentScan("java.risknucleus") in the same structure.

Related