说明:邮件发送是一个很普遍的功能,springboot整合了相关的starter,以下方式可以实现一个简单的邮件发送功能。
1、引入依赖
- mail依赖,发送邮件使用
- test依赖,测试使用
<!-- email -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- test -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>compile</scope>
</dependency>
2、配置yml文件
spring:mail:host: smtp.qq.comport: 25protocol: smtpusername: 123456789@qq.compassword: oujhlavdjhirgiee default-encoding: UTF-8
注意:password属性是邮箱授权码,用于第三方接口调用使用, 而不是邮箱登录密码,需要登录邮箱查看或者申请获取。下面是qq邮箱授权码获取方式(设置->账号->账号安全)
3、编写测试代码
这里主要测试了发送两种格式的邮件,一种是纯文字的邮件,另一种是带有附件的邮件。
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;@SpringBootTest
class EmailUtilsTest {// 发件人邮箱@Value("${spring.mail.username}")private String mailFrom;// 收件人邮箱private static String MAIL_TO = "zhangsan123@163.com";// 测试发送PDF文件的路径private static String PDF_PATH = "E:\\test.pdf";@Autowiredprivate JavaMailSender mailSender;/*** 测试发送文字内容*/@Testvoid testSendText() {// 邮件标题String subject = "测试邮件";// 邮件文字内容String textContent = "你好,这是测试邮件4";MimeMessage message = mailSender.createMimeMessage();try {MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(mailFrom);helper.setTo(MAIL_TO);helper.setSubject(subject);helper.setText(textContent, true);mailSender.send(message);} catch (MessagingException e) {throw new RuntimeException(e);}}/*** 测试发送文件内容*/@Testvoid testSendFile() {// 邮件标题String subject = "一份测试发送的PDF文件";// 邮件文本内容String textContent = "这是您的文件,请注意查收";// 邮件文件内容FileSystemResource pdfFile = new FileSystemResource(PDF_PATH);// 文件名字String fileName = "测试文件.pdf";MimeMessage message = mailSender.createMimeMessage();try {MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(mailFrom);helper.setTo(MAIL_TO);helper.setSubject(subject);helper.setText(textContent, true);// 如果一次要发送多个附件, 只需要添加多个 helper.addAttachment() 即可helper.addAttachment(fileName, pdfFile);mailSender.send(message);} catch (MessagingException e) {throw new RuntimeException(e);}}/*** 扩展: 不使用yml文件, 使用对象的方式设置属性值*/@Testvoid testSendFileNoYml() {// 邮件标题String subject = "一份测试发送的PDF文件";// 邮件文本内容String textContent = "这是您的文件,请注意查收";// 邮件文件内容FileSystemResource pdfFile = new FileSystemResource(PDF_PATH);// 文件名字String fileName = "测试文件.pdf";// 获取自定义属性的senderJavaMailSender javaMailSender = getJavaMailSender();MimeMessage message = javaMailSender.createMimeMessage();try {MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(mailFrom);helper.setTo(MAIL_TO);helper.setSubject(subject);helper.setText(textContent, true);// 如果一次要发送多个附件, 只需要添加多个 helper.addAttachment() 即可helper.addAttachment(fileName, pdfFile);mailSender.send(message);} catch (MessagingException e) {throw new RuntimeException(e);}}/*** 获取自定义的属性的 JavaMailSender 对象* @return JavaMailSender*/public JavaMailSender getJavaMailSender(){JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();// 设置发送的服务器ip (qq:smtp.qq.com 网易:smtp.126.com)javaMailSender.setHost("qq:smtp.qq.com");javaMailSender.setUsername("1454376161@qq.com");// 邮箱授权码javaMailSender.setPassword("oujhldvdjhirgiea");// 设置端口(默认25, 或者设置456)javaMailSender.setPort(465);// 设置编码方式,避免常出现乱码 javaMailSender.setDefaultEncoding("UTF-8");//其他属性Properties p = new Properties();p.setProperty("mail.smtp.auth", "true");p.setProperty("mail.smtp.ssl", "true");p.setProperty("mail.smtp.socketFactory.port", "465");p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");p.setProperty("mail.smtp.starttls.enable", "true");p.setProperty("mail.smtp.starttls.required", "true");javaMailSender.setJavaMailProperties(p);return javaMailSender;}
}