springboot群发邮件问题实例

  先看怎么在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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值