栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot实现邮件发送

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot实现邮件发送

目录

需求背景

实现过程

开启POP3/SMTP服务

书写发送邮件部分代码

测试结果

接口请求

发送成功

异常示例

邮箱授权码错误

邮箱服务器地址错误

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

邮箱未开启POP3/SMTP服务

总结


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

实现过程

开启POP3/SMTP服务
  • 打开网站登录qq邮箱

登录QQ邮箱

  • 开启POP3/SMTP服务

书写发送邮件部分代码
  • 引入依赖

	 
	    org.springframework.boot
	    spring-boot-starter-mail
	 
  • 在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(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 (十):邮件服务 - 纯洁的微笑博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/976009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号