SpringBoot实现邮件发送

目录

需求背景

实现过程

开启POP3/SMTP服务

书写发送邮件部分代码

测试结果

接口请求

发送成功

异常示例

邮箱授权码错误

邮箱服务器地址错误

使用邮箱密码而非生成的授权码

邮箱未开启POP3/SMTP服务

总结


需求背景

  • 因项目需求,需要实现Java发送邮件预警,这里简单的做一个spring boot项目实现Java发送邮件示例(简单调通); 
  • 发送邮箱:xxx@qq.com,配置项都和邮箱有关,比如qq邮箱发送和网易邮箱发送的配置就不一样;
  • 接受邮箱:不规定;
  • 项目是sprint boot项目;
  • 文章不列出踩过的坑,按照正确的步骤一把过。

实现过程

开启POP3/SMTP服务

  • 打开网站登录qq邮箱

登录QQ邮箱

  • 开启POP3/SMTP服务

书写发送邮件部分代码

  • 引入依赖
<dependencies>
	<dependency> 
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-mail</artifactId>
	</dependency> 
</dependencies>
  • 在application.yml中添加邮箱配置
spring:
  mail:
    host: smtp.qq.com #邮箱服务器地址(其他非qq邮箱服务器地址需要对应修改)
    username: 11xxxxxx50@qq.com #用户名
    password: jchdzdeppdtjxhbhe #密码
    default-encoding: UTF-8

mail:
  fromMail.addr: 11xxxxxx50@qq.com  #以谁来发送邮件

 上图中的password需要登录邮箱生成,如下(按照提示发送短信就会生成):

  • 基本类:Mail
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Mail {

    private String to;

    private String subject;

    private String content;
}
  • 服务层接口:MailService
public interface MailService {

    void sendSimpleMail(Mail mail);
}
  • 服务层实现类:MailServiceImpl
@Service
public class MailServiceImpl implements MailService {

    final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JavaMailSender mailSender;

    @Value("${mail.fromMail.addr}")
    private String from;

    @Override
    public void sendSimpleMail(Mail mail) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(mail.getTo());
        message.setSubject(mail.getSubject());
        message.setText(mail.getContent());

        try {
            mailSender.send(message);
            logger.info("简单邮件已经发送。");
        } catch (Exception e) {
            logger.error("发送简单邮件时发生异常!", e);
        }
    }

}
  • 控制层:MailController
@RestController
@RequestMapping("/mail")
public class TestController {

    @Autowired
    private MailServiceImpl mailService;

    @GetMapping("/send")
    public void sendMail(@RequestBody Mail mail) {
        mailService.sendSimpleMail(mail);
    }
}

测试结果

接口请求

发送成功

异常示例

邮箱授权码错误

org.springframework.mail.MailAuthenticationException: Authentication failed; 
    nested exception is javax.mail.AuthenticationFailedException: 535 Login Fail. 
    Please enter your authorization code to login. 
    More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

邮箱服务器地址错误

org.springframework.mail.MailAuthenticationException: Authentication failed; 
    nested exception is javax.mail.AuthenticationFailedException: 535 Error: authentication failed

使用邮箱密码而非生成的授权码

org.springframework.mail.MailAuthenticationException: Authentication failed; 
    nested exception is javax.mail.AuthenticationFailedException: 535 Login Fail. 
    Please enter your authorization code to login. 
    More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

邮箱未开启POP3/SMTP服务

org.springframework.mail.MailAuthenticationException: Authentication failed; 
    nested exception is javax.mail.AuthenticationFailedException: 535 Login Fail. 
    Please enter your authorization code to login. 
    More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

注意:"邮箱授权码错误"和"邮箱未开启POP3/SMTP服务"错误一样。

总结

注意两个地方:

  • 邮箱对应的服务器地址必须要匹配,不知道可以去网上查;
  • 代码里配置的邮箱password必须是POP3/SMTP服务下生成的授权码,写实际邮箱密码会报异常 ;
  • 授权码一经生成,关闭POP3/SMTP服务后再开启,授权码依然有效(所以开启POP3/SMTP服务邮箱就存在安全风险)。

更多更详细可参考:Spring Boot (十):邮件服务 - 纯洁的微笑博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值