引入依赖
<!-- 父配置--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.3</version><relativePath/></parent>......<!-- springboot--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 邮件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
配置文件:
spring:mail:# 配置 SMTP 服务器地址host: smtp.qq.com# 发送者邮箱username: ooahz@qq.com# 邮箱密码(即邮件授权码)password: axxxxxxxxz# 邮箱端口号(QQ邮箱为465或587)port: 587# 默认的邮件编码为UTF-8default-encoding: UTF-8# 配置SSL 加密工厂properties:mail:smtp:socketFactoryClass: javax.net.ssl.SSLSocketFactory# 开启Debugdebug: true
启动类:
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;@EnableScheduling@SpringBootApplicationpublic class Application {public static void main(String[] args){SpringApplication.run(Application.class, args);}}
携带文字发送
接口类:
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Date;/*** @author ahzoo* @create 2022/2/11* @desc 邮件发送*/@RestControllerpublic class email {@AutowiredJavaMailSender javaMailSender;/*** 发送简单邮件* @return success*/@PostMapping("/send")public String sendEmail() {// 构建邮件发送对象SimpleMailMessage message = new SimpleMailMessage();// 设置邮件主题message.setSubject("这是一封测试邮件");// 设置邮件发送者,与配置文件中的邮箱保持一致message.setFrom("ooahz@qq.com");// 设置邮件接收者,可以有多个接收者,中间用逗号隔开,以下类似// message.setTo("123@qq.com","999qq.com");message.setTo("ooahz@qq.com");// 设置邮件抄送人,可以有多个抄送人// message.setCc("999@163.com");// // 设置隐秘抄送人,可以有多个// message.setBcc("999@outlook.com");// 设置邮件发送日期message.setSentDate(new Date());// 设置邮件的正文message.setText("这是一封测试邮件————from Ahzoo");// 发送邮件javaMailSender.send(message);return "发送成功";}}
因为开启了debug功能,所以邮件发送结果会在控制台打印:
测试发送:
携带附件发送
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;import java.util.Date;/*** @author ahzoo* @create 2022/2/11* @desc 邮件发送*/@RestControllerpublic class email {@AutowiredJavaMailSender javaMailSender;/*** 携带附件发送** @return result*/@PostMapping("/sendAttachment")public String sendFile() {// 使用MimeMessage对象发送复杂邮件MimeMessage mimeMessage = javaMailSender.createMimeMessage();// 使用MimeMessageHelper对邮件进行配置,true表示构建一个可以带附件的邮件对象MimeMessageHelper helper = null;try {helper = new MimeMessageHelper(mimeMessage, true);helper.setSubject("测试附件");helper.setFrom("ooahz@qq.com");helper.setTo("ooahz@qq.com");// helper.setCc("999@163.com");// helper.setBcc("999@outlook.com");helper.setSentDate(new Date());helper.setText("这是一封测试邮件————from Ahzoo");File path = new File(ResourceUtils.getURL("classpath:").getPath());// 第一个参数是自定义的名称,后缀需要加上,第二个参数是文件的位置helper.addAttachment("文件.txt", new File(path + "/static/z.txt"));javaMailSender.send(mimeMessage);return "发送成功";} catch (Exception e) {e.printStackTrace();return "发送失败" + e.getMessage();}}}
携带静态资源发送
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;import java.util.Date;/*** @author ahzoo* @create 2022/2/11* @desc 邮件发送*/@RestControllerpublic class email {@AutowiredJavaMailSender javaMailSender;/*** 携带静态资源发送** @return*/@PostMapping("sendResource")public String sendImg() {MimeMessage mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper helper = null;try {helper = new MimeMessageHelper(mimeMessage, true);helper.setSubject("测试静态资源");helper.setFrom("ooahz@qq.com");helper.setTo("ooahz@qq.com");// helper.setCc("999@163.com");// helper.setBcc("999@outlook.com");helper.setSentDate(new Date());// 设置邮件正文内容。cid为占位符,对应下面的contentId。true表示正文为Htmlhelper.setText("<p>图片1:</p><img src='cid:image1'/><p>图片2:</p><img src='cid:image1'/>", true);// contentId:对应上面占位符中的cid。file:静态资源路径File path = new File(ResourceUtils.getURL("classpath:").getPath());helper.addInline("image1", new File(path + "/static/1.png"));helper.addInline("image2", new File(path + "/static/ahzoo.jpg"));javaMailSender.send(mimeMessage);return "发送成功";} catch (Exception e) {e.printStackTrace();return "发送失败" + e.getMessage();}}}

使用模板发送
常见的模板引擎主要有:Thymeleaf和Freemaker,这里不做演示
后记
项目结构:
报错:
Description:Field javaMailSender in com.music.demo.Controller.UserController required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.The injection point has the following annotations:- @org.springframework.beans.factory.annotation.Autowired(required=true)
原因:配置文件未配置正确
