我在springboot项目中执行发送邮件的方法时报错如下:
org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated; message exception details (1) are:Failed message 1:com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticatedat com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:464)at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:322)at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:311)at com.hotwind.hwstore.service.schedule.impl.Sync4MockImpl.sendMail(Sync4MockImpl.java:75)at com.hotwind.hwstore.service.schedule.impl.Sync4MockImpl.doWork(Sync4MockImpl.java:51)at com.hotwind.hwstore.service.schedule.ScheduleService.syncPutAllStore4Moka(ScheduleService.java:40)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)at java.util.concurrent.FutureTask.run(FutureTask.java:266)at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)
我也参考了不少链接都没有解决,后来我分析“Client was not authenticated”这句话说的是没有通过身份验证,然后我在配置中添加了:spring.mail.properties.mail.smtp.auth=true这句话就解决了。
以下是我的项目代码,实现简单的邮件发送:
在pom.xml文件添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
application.properties中添加配置:
spring.mail.host=10.0.16.7spring.mail.port=25spring.mail.username=xxxxx@xx.netspring.mail.password=xxxspring.mail.default-encoding=UTF-8spring.mail.properties.mail.smtp.auth=true
发送邮件的方法:
public void sendMail(String str) {try {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(\"xxxxx@xx.net\");message.setTo(收件人的邮箱);message.setCc(抄送人的邮箱);message.setSubject(邮件主题);message.setText(邮件内容);javaMailSender.send(message);} catch (Exception e) {e.printStackTrace();}}