如何解决以下发送电子邮件的异常?
how to I solve the following exception to sending the email?
我试图在我的一个项目中使用电子邮件实用程序 class。但是我遇到了以下错误。然后我将它分解为非常基本的代码,但我仍然无法发送电子邮件。
这是我得到的,
服务 class
包 com.test.email.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;
public void sendSimpleEmail(String toEmail, String body, String subject) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("**********************************");
message.setTo(toEmail);
message.setText(body);
message.setSubject(subject);
mailSender.send(message);
System.out.println("Mail Send..........................................");
}
}
这是主要的class
import com.test.email.service.EmailService;
@SpringBootApplication
public class EmailtestApplication {
@Autowired
private EmailService emailService;
public static void main(String[] args) {
SpringApplication.run(EmailtestApplication.class, args);
System.out.println(" Spring boot application is running...................");
}
@EventListener(ApplicationReadyEvent.class)
public void triggerMail() throws MessagingException{
emailService.sendSimpleEmail("spring.boot.test83@gmail.com",
"This is the Email Body with attachement....",
"This is the Email Subject");
}
}
应用程序属性
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=***************************
spring.mail.password=***************************
spring.mail.properties.mail.smtp.strattls.enable=true
spring.mail.properties.mail.smtp.strattls.required=true
spring.mail.properties.mail.smtp.auth=true
那么这就是我遇到的错误
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m [2m (v2.6.7)[0;39m
[2m2022-04-23 11:43:32.112[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mc.test.email.EmailtestApplication [0;39m [2m:[0;39m Starting EmailtestApplication using Java 17.0.2 on gorkhali with PID 5372 (F:\Spring-projects\emailtest\target\classes started by adhik in F:\Spring-projects\emailtest)
[2m2022-04-23 11:43:32.114[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mc.test.email.EmailtestApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2022-04-23 11:43:32.709[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2022-04-23 11:43:32.722[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2022-04-23 11:43:32.722[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.62]
[2m2022-04-23 11:43:32.849[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2022-04-23 11:43:32.849[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 701 ms
[2m2022-04-23 11:43:33.114[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path ''
[2m2022-04-23 11:43:33.121[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mc.kiran.test.email.EmailtestApplication [0;39m [2m:[0;39m Started EmailtestApplication in 1.263 seconds (JVM running for 1.831)
message variable is created
[2m2022-04-23 11:43:33.319[0;39m [31mERROR[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.boot.SpringApplication [0;39m [2m:[0;39m Application run failed
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. e26-20020ac84b5a000000b002f35d11c5dfsm2040437qts.65 - gsmtp
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:491) ~[spring-context-support-5.3.19.jar:5.3.19]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:323) ~[spring-context-support-5.3.19.jar:5.3.19]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:312) ~[spring-context-support-5.3.19.jar:5.3.19]
at com.kiran.test.email.service.EmailService.sendSimpleEmail(EmailService.java:23) ~[classes/:na]
at com.kiran.test.email.EmailtestApplication.triggerMail(EmailtestApplication.java:26) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:344) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:229) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:166) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.boot.context.event.EventPublishingRunListener.ready(EventPublishingRunListener.java:114) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplicationRunListeners.lambda$ready(SpringApplicationRunListeners.java:82) ~[spring-boot-2.6.7.jar:2.6.7]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplicationRunListeners.ready(SpringApplicationRunListeners.java:82) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.7.jar:2.6.7]
at com.kiran.test.email.EmailtestApplication.main(EmailtestApplication.java:20) ~[classes/:na]
您的属性文件中有错字。
正确的属性
spring.mail.properties.mail.smtp.starttls.enable=真
spring.mail.properties.mail.smtp.starttls.required=真
这让我想起了我看到的一个表情包。
我试图在我的一个项目中使用电子邮件实用程序 class。但是我遇到了以下错误。然后我将它分解为非常基本的代码,但我仍然无法发送电子邮件。
这是我得到的, 服务 class
包 com.test.email.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;
public void sendSimpleEmail(String toEmail, String body, String subject) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("**********************************");
message.setTo(toEmail);
message.setText(body);
message.setSubject(subject);
mailSender.send(message);
System.out.println("Mail Send..........................................");
}
}
这是主要的class
import com.test.email.service.EmailService;
@SpringBootApplication
public class EmailtestApplication {
@Autowired
private EmailService emailService;
public static void main(String[] args) {
SpringApplication.run(EmailtestApplication.class, args);
System.out.println(" Spring boot application is running...................");
}
@EventListener(ApplicationReadyEvent.class)
public void triggerMail() throws MessagingException{
emailService.sendSimpleEmail("spring.boot.test83@gmail.com",
"This is the Email Body with attachement....",
"This is the Email Subject");
}
}
应用程序属性
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=***************************
spring.mail.password=***************************
spring.mail.properties.mail.smtp.strattls.enable=true
spring.mail.properties.mail.smtp.strattls.required=true
spring.mail.properties.mail.smtp.auth=true
那么这就是我遇到的错误
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m [2m (v2.6.7)[0;39m
[2m2022-04-23 11:43:32.112[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mc.test.email.EmailtestApplication [0;39m [2m:[0;39m Starting EmailtestApplication using Java 17.0.2 on gorkhali with PID 5372 (F:\Spring-projects\emailtest\target\classes started by adhik in F:\Spring-projects\emailtest)
[2m2022-04-23 11:43:32.114[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mc.test.email.EmailtestApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2022-04-23 11:43:32.709[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2022-04-23 11:43:32.722[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2022-04-23 11:43:32.722[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.62]
[2m2022-04-23 11:43:32.849[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2022-04-23 11:43:32.849[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 701 ms
[2m2022-04-23 11:43:33.114[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 8080 (http) with context path ''
[2m2022-04-23 11:43:33.121[0;39m [32m INFO[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mc.kiran.test.email.EmailtestApplication [0;39m [2m:[0;39m Started EmailtestApplication in 1.263 seconds (JVM running for 1.831)
message variable is created
[2m2022-04-23 11:43:33.319[0;39m [31mERROR[0;39m [35m5372[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.boot.SpringApplication [0;39m [2m:[0;39m Application run failed
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. e26-20020ac84b5a000000b002f35d11c5dfsm2040437qts.65 - gsmtp
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:491) ~[spring-context-support-5.3.19.jar:5.3.19]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:323) ~[spring-context-support-5.3.19.jar:5.3.19]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:312) ~[spring-context-support-5.3.19.jar:5.3.19]
at com.kiran.test.email.service.EmailService.sendSimpleEmail(EmailService.java:23) ~[classes/:na]
at com.kiran.test.email.EmailtestApplication.triggerMail(EmailtestApplication.java:26) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:344) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:229) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:166) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.boot.context.event.EventPublishingRunListener.ready(EventPublishingRunListener.java:114) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplicationRunListeners.lambda$ready(SpringApplicationRunListeners.java:82) ~[spring-boot-2.6.7.jar:2.6.7]
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplicationRunListeners.ready(SpringApplicationRunListeners.java:82) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.7.jar:2.6.7]
at com.kiran.test.email.EmailtestApplication.main(EmailtestApplication.java:20) ~[classes/:na]
您的属性文件中有错字。
正确的属性
spring.mail.properties.mail.smtp.starttls.enable=真 spring.mail.properties.mail.smtp.starttls.required=真
这让我想起了我看到的一个表情包。