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

huangapple go评论79阅读模式
英文:

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

问题

我正在实现如何使用Spring Boot发送电子邮件
我正在尝试在Visual Studio Code中实现这个。
但是它给我以下错误

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

我在我的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"})注解有关

有人可以帮助我解决这个错误吗?

英文:

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

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

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

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
&lt;/dependency&gt;

my main bootstrap class:

   @SpringBootApplication
  @ComponentScan(basePackages = {
  &quot;email&quot;
  })

   public class Application {
   public static void main(String[] args) {

	
	Mail mail = new Mail();
	
        mail.setMailFrom(&quot;abc@gmail.com&quot;);
        mail.setMailTo(&quot;xyz@gmail.com&quot;);
        mail.setMailSubject(&quot;Hi&quot;);
		mail.setMailContent(&quot;Hope you are doing well&quot;);
		
	
		ApplicationContext ctx = SpringApplication.run(Application.class, 
     args);
		MailService mailService = (MailService) ctx.getBean(&quot;mailService&quot;);
		
	  mailService.sendEmail(mail);	

	
     }

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

Can anyone help me with the error?

答案1

得分: 4

因为我们不知道包的结构,所以很难确定在@ComponentScan内的basePackages中应该有什么。

首先,请将您的应用程序类移到包结构的更高层,这样它将默认读取其下的所有包,并删除组件扫描中的basePackages。所以,应该只是@ComponentScan

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

尝试这样做,然后告诉我们,同时我希望您有@Service注解为@Service("mailService")

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

他将类移到了更高层,并删除了basePackages,这对他起作用。正如我在答案的第一部分中所述。

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

英文:

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(&quot;mailService&quot;)

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 = {&quot;email&quot;}) to @ComponentScan(&quot;java.risknucleus&quot;) in the same structure.

huangapple
  • 本文由 发表于 2020年7月21日 20:02:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63014126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定