先看怎么在spring boot发邮件,我们通过一个service来实现:
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class MailService {
@Autowired
JavaMailSender javaMailSender;
@Value("${mail.from}")
private String mailFrom;
@Value("${mail.to}")
private String mailTo;
@Async
public void sendSimpleMail(String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(mailFrom);
message.setTo(mailTo);
message.setSubject(subject);
message.setText(content);
try {
javaMailSender.send(message);
log.info("邮件已经发送");
} catch (Exception e) {
log.error("发送邮件异常", e);
}
}
}
我们在application.properties文件中配置邮件参数,给mait.to配置多个邮箱,并用分号分隔
#邮件
spring.mail.host=smtp.163.com
spring.mail.username=tangseng2019@163.com
spring.mail.password=wlf123
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.starttls.required=true
spring.mail.properties.mail.starttls.enable=true
spring.mail.default-encoding=UTF-8
mail.from=${spring.mail.username}
mail.to=sunwukong2019@126.com;zhubajie2019@126.com
注意spring.mail.password不是邮箱密码,而是授权码,需要在邮箱的“设置”中启用授权码:
调用上面的发送接口sendSimpleMail
问题来了:
2020-01-02 16:29:00.549 |-ERROR [http-nio-9988-exec-6] com.wlf.order.prize.service.MailService [34] -| 发送邮件异常
org.springframework.mail.MailParseException: Could not parse mail; nested exception is javax.mail.internet.AddressException: Illegal semicolon, not in group in string ``sunwukong2019@126.com;zhubajie2019@126.com'' at position 17
at org.springframework.mail.javamail.MimeMailMessage.setTo(MimeMailMessage.java:111)
at org.springframework.mail.SimpleMailMessage.copyTo(SimpleMailMessage.java:204)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:319)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:311)
at com.wlf.order.prize.service.MailService.sendSimpleMail(MailService.java:31)
at com.wlf.order.prize.aop.RequestInterceptor.postHandle(RequestInterceptor.java:117)
at org.springframework.web.servlet.HandlerExecutionChain.applyPostHandle(HandlerExecutionChain.java:156)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1046)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.mail.internet.AddressException: Illegal semicolon, not in group
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:1045)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:752)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:729)
at org.springframework.mail.javamail.MimeMessageHelper.parseAddress(MimeMessageHelper.java:710)
at org.springframework.mail.javamail.MimeMessageHelper.setTo(MimeMessageHelper.java:596)
at org.springframework.mail.javamail.MimeMailMessage.setTo(MimeMailMessage.java:108)
... 50 common frames omitted
2020-01-02 16:29:00.550 |-DEBUG [http-nio-9988-exec-6] org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor [111] -| Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
我们跟进MimeMailMessage类,发现最终死在了InternetAddress手上
非法分号,也就是群发我们不应该调setTo(String to),而应该是setTo(String... to),我们需要自己把分号拆分一下,统一起来就是:
自此开始批发邮件了,但问题又来了,邮件服务器开始把你的邮件当垃圾邮件来处理了。详情件spring发邮件被识别为垃圾邮件:org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163。