springboot发邮件问题实例  先看怎么在spring boot发邮件,我们通过⼀个service来实现:
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("${}")
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) {
<("发送邮件异常", e);
}
}
}
  我们在application.properties⽂件中配置邮件参数,给配置多个邮箱,并⽤分号分隔
#邮件
spring.mail.host=smtp.163
spring.mail.username=tangseng2019@163
spring.mail.password=wlf123
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.quired=true
spring.mail.properties.able=true
spring.mail.default-encoding=UTF-8
mail.from=${spring.mail.username}
<=sunwukong2019@126;zhubajie2019@126
  注意spring.mail.password不是邮箱密码,⽽是授权码,需要在邮箱的“设置”中启⽤授权码:
  调⽤上⾯的发送接⼝sendSimpleMail
  问题来了:
2020-01-0216:29:00.549 |-ERROR [http-nio-9988-exec-6] der.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;zhubajie2019@126'' at position    at org.springframework.mail.javamail.MimeMailMessage.setTo(MimeMailMessage.java:111)
at org.springframework.pyTo(SimpleMailMessage.java:204)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:319)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:311)
at der.prize.service.MailService.sendSimpleMail(MailService.java:31)
at der.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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at at.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.tor.CoyoteAdapter.service(CoyoteAdapter.java:343)springboot aop
at http11.Http11Processor.service(Http11Processor.java:408)
at AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
at at.util.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
at at.util.SocketProcessorBase.run(SocketProcessorBase.java:49)
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at at.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-0216:29:00.550 |-DEBUG [http-nio-9988-exec-6] jpa.support.OpenEntityManagerInViewInterceptor [111] -| Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
  我们跟进MimeMailMessage类,发现最终死在了InternetAddress⼿上
  ⾮法分号,也就是发我们不应该调setTo(String to),⽽应该是 to),我们需要⾃⼰把分号拆分⼀下,统⼀起来就是:
 ⾃此开始批发邮件了,但问题⼜来了,邮件服务器开始把你的邮件当垃圾邮件来处理了。详情件。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。