SpringBoot集成QQ邮箱发送邮件
303 字
2 分钟
SpringBoot集成QQ邮箱发送邮件
1.导入依赖
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.6</version></dependency>2.获取QQ邮箱16位SMTP口令
拉到此处(下图所示)

3.添加发送邮件的类(所有导包都是javax.mail下的包)
public void sendEmail(String email){ //创建Properties类 用于记录邮箱的一些属性 Properties props = new Properties();
//表示SMTP发送邮件 必须进行身份验证 props.put("mail.smtp.auth","true"); //此处填写SMTP服务器 props.put("mail.smtp.host","smtp.qq.com"); //端口号 QQ端口号 587 props.put("mail.smtp.port","587"); //此处填写写信人账号 props.put("mail.user","写信人账号"); //此处填写16位SMTP口令 props.put("mail.password","16位SMTP口令");
//构建授权信息 用于SMTP进行身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { //用户名密码 String username = (String) props.get("mail.user"); String password = (String) props.get("mail.password"); return new PasswordAuthentication(username,password); } }; //使用环境属性和授权信息 创建邮件会话 Session mailSession = Session.getInstance(props, authenticator); //创建邮件消息 MimeMessage mimeMessage = new MimeMessage(mailSession); //设置发件人 InternetAddress form = null; try { form = new InternetAddress(props.get("mail.user").toString()); mimeMessage.setFrom(form);
InternetAddress to = new InternetAddress(email); mimeMessage.setRecipients(Message.RecipientType.TO, String.valueOf(to));
//设置邮件标题 mimeMessage.setSubject("标题");
//设置邮件内容体 String content = "内容" mimeMessage.setContent(content,"text/html;charset=UTF8");
//发送邮件 Transport.send(mimeMessage); redisTemplate.opsForValue().set(email,code,15L,TimeUnit.MINUTES);
} catch (MessagingException e) { e.printStackTrace(); } }文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
SpringBoot集成QQ邮箱发送邮件
https://czxcly.cn/posts/springboot集成qq邮箱发送邮件/ 相关文章 智能推荐
1
SpringBoot项目[禁止OPTIONS等不安全的请求]增加OPTIONS不安全请求处理
技术文章 SpringBoot项目[禁止OPTIONS等不安全的请求]增加OPTIONS不安全请求处理
2
ConfigurationProperties无法将yml或properties中的内容读取到bean解决方法
技术文章 ConfigurationProperties无法将yml或properties中的内容读取到bean解决方法
3
多模块项目公共类配置文件加载方法
技术文章 多模块项目公共类配置文件加载方法
4
springboot-jar包启动提示没有主清单属性
技术文章 springboot-jar包启动提示没有主清单属性
5
彻底卸载谷歌浏览器残留
其它教程 彻底卸载谷歌浏览器残留
随机文章 随机推荐