【SpringBoot整合JavaMail发送邮件】
一 . Java Mail基本概念
1.SMTP
Simple Mail Transfer Protocol:简单邮件传输协议,用于发送邮件的协议。
2.POP3
Post office Protocol 3:邮局通讯协议第三版,用于接收邮件的标准协议。
3.IMAP
Internet Message Access Protocol:互联网消息访问协议,是POP3的替代。
二、准备工作
1.注册邮箱 - 126邮箱
2.登陆授权码
主界面->设置->POP3/SMTP/IMAPA
开启服务 | 重启服务
注册手机号扫码->发送指定短信内容->查看授权码
三.开发简单邮件
1.什么叫简单邮件
仅包含邮件标题、邮件正文的普通邮件。
2.引入mail启动器
3.配置mail参数
Spring.mail.username =****** #填写个人真实的mail地址
spring.mail.password =****** #填写授权码(注意不是原始密码)
spring.mail.host =******* #根据具体邮件平台填写发送邮件的host(smtp.126.com)
4.API
JavaMailSender: 发送邮件的客户端
send(mail)
SimpleMailMessage:封装简单邮件的Bean
setsubject(标题)|setText(正文)|setTo(收件地址)|setFrom(发件地址)
四.开发复杂邮件:
1.什么是复杂邮件
区别于简单邮件,复杂邮件可附加更多内容,例如:正文中插入图片或链接、邮件带附件等。
2.API
JavaMailsender: 发送邮件的客户端
send(mail)
MimeMessage:封装复杂邮件的Bean
sender.createMimeMessage()
MimeMessageHelper:设置复杂邮件的辅助工具
setSubiect(标题) | setText(正文,escapeHtml) | setTo(收件地址) | setFrom(发件地址) | addAttachment(fileName,file)
【代码】
1、简单邮件发送:
spring:mail:
# 账号username: "1xxxx@163.com"
# 授权码password: "xxxxx"host: "smtp.163.com"
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;@SpringBootTest
class SendEmailApplicationTests {
@Autowired
private JavaMailSender javaMailSender;
private String subject ="邮件标题";
private String content ="邮件正文";
//收件人邮箱
private String to ="xxxx@163.com";
//发送人邮箱
private String from =to;@Testvoid contextLoads() {SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject(subject);mailMessage.setTo(to);mailMessage.setFrom(from);mailMessage.setText(content);javaMailSender.send(mailMessage);System.out.println("邮件发送成功!");}}
结果:
1、复杂邮件发送:
package com.example;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;@SpringBootTest
class SendEmailApplicationTests {
@Autowired
private JavaMailSender javaMailSender;
private String subject ="邮件标题";
private String content ="邮件正文";
private String to ="xxxxx@163.com";
private String from =to;/*** 复杂邮件发送测试方法* 正文中添加图片链接,附件等*/@Testvoid contextLoads1() throws MessagingException {MimeMessage mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);mimeMessageHelper.setSubject(subject);mimeMessageHelper.setTo(to);mimeMessageHelper.setFrom(from);mimeMessageHelper.setText(content+"<img src=https://img2.baidu.com/it/u=3280201741,1314329474&fm=253&fmt=auto&app=138&f=JPEG?w=608&h=1083>", true);mimeMessageHelper.addAttachment("附件1", new File("C:\\Users\\ww\\Pictures\\Saved Pictures\\Wallpaper\\programmer_1920.png"));javaMailSender.send(mimeMessage);System.out.println("邮件发送成功!");}}
出现这个错误:
解决办法:
结果: