springboot群发邮件问题实例

Easter79
• 阅读 428

  先看怎么在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不是邮箱密码,而是授权码,需要在邮箱的“设置”中启用授权码:

springboot群发邮件问题实例

  调用上面的发送接口sendSimpleMail

springboot群发邮件问题实例

  问题来了:

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手上

springboot群发邮件问题实例

 springboot群发邮件问题实例

  非法分号,也就是群发我们不应该调setTo(String to),而应该是setTo(String... to),我们需要自己把分号拆分一下,统一起来就是:

springboot群发邮件问题实例

  自此开始批发邮件了,但问题又来了,邮件服务器开始把你的邮件当垃圾邮件来处理了。详情件spring发邮件被识别为垃圾邮件:org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 163

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
sprintboot
一、邮件发送使用springboot自带的邮件系统就能实现邮件的发送,首先导入依赖:1、新建springboot项目,添加依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstartermail
Karen110 Karen110
3年前
赶在元旦前夕!10分钟用Python批量定制化发送元旦祝福邮件!
上一篇关于邮件的自动发送教程中,我们讲解了如何使用yagmail进行简单的邮件发送:现实生活中,如果只是发邮件给自己,像上一篇文章那样简陋的格式是可以接受的,但若要针对每个人进行邮件的定制化,群发给公司客户、内部员工、亲戚朋友,则需更加高级的邮件发送方式。我们可以通过HTML制作一封精美的元旦祝福邮件,但是邮件的内容——比如姓名、祝福语等应该怎样动态
Stella981 Stella981
3年前
Spring Boot中使用JavaMailSender发送邮件
相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送。在SpringBoot的Starter模块中也为此提供了自动化配置。下面通过实例看看如何在SpringBoot中使用JavaMailSender发送邮件。快速入门在SpringBoot的工程中的pom.xm
Stella981 Stella981
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Stella981 Stella981
3年前
Spring Boot 与 Kotlin 定时任务(Scheduling Tasks)
在编写SpringBoot应用中会遇到这样的场景,比如:需要定时地发送一些短信、邮件之类的操作,也可能会定时地检查和监控一些标志、参数等。创建定时任务在SpringBoot中编写定时任务是非常简单的事,下面通过实例介绍如何在SpringBoot中创建定时任务,实现每过5秒输出一下当前时间。在SpringBoot的主类中加入
Stella981 Stella981
3年前
IE7、IE8、IE9对min
问题:    IE7、IE8、IE9对minheight不识别,其他无问题解决:   box{width:100px;height:35px;}   htmlbodybox{width:auto;height:auto;width:100px;minheight:35px;} 实例:
Easter79 Easter79
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k