admin 管理员组

文章数量: 887021

SpringBoot发送邮件,发送二维码,邮箱验证,图片,Html,附件等

1、准备工作

以刚刚创建的SpringBoot项目为主:
以QQ邮箱为例:(其实什么邮箱都差不多)

1.进入qq邮箱设置

2.找到账户

3.往下滑,找到pop3服务,点击开启,需要验证,得到授权码


2、配置代码

1导入依赖

		<!-- 发送邮箱所需依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
2.配置配置文件,application.properties(重点)

#host 代表使用什么邮箱, QQ:smtp.qq  163:smtp.163
#default-encoding 字符集 默认utf-8
#username 邮箱账号
#password 第一步的授权码
spring.mail.host=smtp.qq
spring.mail.default-encoding=utf-8
spring.mail.username=xxxx
spring.mail.password=xxxx

username是填写邮箱账号
password不是填写密码,是填写第一步的授权码

3.创建发送邮件实现类接口(重点)

复制进去导入包即可

package com.lj.demo.mail;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;@Component
public class MailServiceImpl {@Autowiredprivate JavaMailSender mailSender;@Value("${spring.mail.username}")private String from;/*** 发送普通文本邮件*/public void sendSimpleMail(String to, String subject, String content) throws MailException {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from); // 邮件发送者message.setTo(to); // 邮件接受者message.setSubject(subject); // 主题message.setText(content); // 内容mailSender.send(message);}/*** 发送带图片邮件邮件*/public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);File file = new File(rscPath);FileSystemResource res = new FileSystemResource(file);helper.addInline(rscId, res);mailSender.send(message);}/*** 发送HTML邮件*/public void sendHtmlMail(String to, String subject, String content) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();//true 表⽰示需要创建⼀一个 multipart messageMimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);mailSender.send(message);}/*** 发送带附件的邮件*/public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);FileSystemResource file = new FileSystemResource(new File(filePath));String fileName = file.getFilename();helper.addAttachment(fileName, file);mailSender.send(message);}}

接下来我们测试,在测试类中进行测试,实际开发模仿我的示例

package com.lj.demo;import com.lj.demo.mail.MailServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.MailException;import javax.mail.MessagingException;@SpringBootTest
class DemoApplicationTests {@AutowiredMailServiceImpl mailService;/*** 普通文本邮件测试** @throws Exception*/@Testpublic void test() throws Exception {String to = "邮件名@qq";String subject = "主题";String content = "内容";try {mailService.sendSimpleMail(to, subject, content);System.out.println("成功了");} catch (MailException e) {System.out.println("失败了");e.printStackTrace();}}/*** 带图片的邮件测试*/@Testpublic void test1() {String to = "邮件名@163";String subject = "主题";String rscId = "img";String content = "<html><body><img width='250px' src=\'cid:" + rscId + "\'></body></html>";// 此处为电脑系统路径String imgPath = "C:\\Users\\Administrator\\Desktop\\0f819bedc81e4447a79ebc0207a5704e.png";try {mailService.sendInlineResourceMail(to, subject, content, imgPath, rscId);System.out.println("成功了");} catch (MessagingException e) {System.out.println("失败了");e.printStackTrace();}}/*** 带HTML邮件测试*/@Testpublic void test2() {String to = "邮件名@qq";String subject = "主题";String content = "<html><head></head><body><h3>哈哈,什么都没有</h3></body></html>";try {mailService.sendHtmlMail(to, subject, content);System.out.println("成功了");} catch (MessagingException e) {System.out.println("失败了");e.printStackTrace();}}/*** 带附件的邮件测试*/@Testpublic void test3() {String to = "邮件名@163";String subject = "附件的邮件";String content = "内容";String imgPath = "C:\\Users\\Administrator\\Desktop\\0f819bedc81e4447a79ebc0207a5704e.png";try {mailService.sendAttachmentsMail(to, subject, content, imgPath);System.out.println("成功了");} catch (MessagingException e) {System.out.println("失败了");e.printStackTrace();}}}

填写好接收的邮箱,全部测试

完成

有可能您还需要

SpringBoot发送短信验证码:
Java代码生成二维码:
如果需要邮箱发送二维码,可参考我Java代码生成二维码配合此文章可实现

本文标签: SpringBoot发送邮件,发送二维码 邮箱验证 图片 HTML 附件等