【SpringBoot】十四、SpringBoot中发送邮件详解
发布日期:2021-06-30 21:30:05 浏览次数:2 分类:技术文章

本文共 4327 字,大约阅读时间需要 14 分钟。

前言:通过前面快速学习了SpringBoot之后,便很快爱上了SpringBoot,今天学习了使用SpringBoot发送邮件的相关知识,便想着记录一下。

  • 一、首先,我们需要在pom.xml文件中引入发送邮件的依赖
org.springframework.boot
spring-boot-starter-mail
org.springframework.boot
spring-boot-starter-thymeleaf

我们除了引入发送邮件的依赖,我们还引入了thymeleaf模板引擎依赖,后面我们会使用它来发送模板邮件。

  • 二、在配置文件application.yml文件中写入发送邮件的配置信息
#字符集    spring.mail.default-encoding=UTF-8    #电子邮件地址    spring.mail.host=smtp.qq.com    #端口    spring.mail.port=587    #授权密码(不是登陆密码)    spring.mail.password=xxxxxxxxxxxxxxxx    #邮箱账号名    spring.mail.username=xxxxxxxxxx@qq.com    #SSL 加密工厂    spring.mail.properties.mail.smtp.socketFactoryClass=javax.net.ssl.SSLSocketFactory    #开启debug模式    spring.mail.properties.mail.debug=true

这里我是使用QQ邮箱来测试发送邮件的,你需要在QQ邮箱官网获取你的邮箱授权码,网上都有教程的。

  • 三、编写发送邮件方法

1、发送文本邮件

public void sendSimpleMail(String fromAddress,String to, String subject, String content) {	// 编辑发送邮件的一些信息,有-->发件人地址,收件人地址,邮件标题,邮件正文    SimpleMailMessage message = new SimpleMailMessage();    // 发件人地址    message.setFrom(fromAddress);    // 收件人地址    message.setTo(to);    // 邮件标题    message.setSubject(subject);    // 邮件正文    message.setText(content);    try {    	// 使用该类的send()方法发送邮件        mailSender.send(message);//private JavaMailSender mailSender;        System.out.println("发送成功!");    }catch (Exception e){        System.out.println("发送失败!");        System.out.println(e.getMessage());    }}

2、发送HTML邮件

public void sendHtmlMail(String fromAddress,String to, String subject, String content) throws MessagingException {    MimeMessage message=mailSender.createMimeMessage();    // 这里与发送文本邮件有所不同    MimeMessageHelper helper = new MimeMessageHelper(message,true);    helper.setFrom(fromAddress);    helper.setTo(to);    helper.setSubject(subject);    // 发送HTML邮件,也就是将邮件正文使用HTML的格式书写    helper.setText(content,true);    try {        mailSender.send(message);        System.out.println("发送成功!");    }catch (Exception e){        System.out.println("发送失败!");        System.out.println(e.getMessage());    }}

3、发送附件邮件

public void sendAttachmentMail(String fromAddress,String to, String subject, String content, String filePath) throws MessagingException {    MimeMessage message=mailSender.createMimeMessage();    MimeMessageHelper helper = new MimeMessageHelper(message,true);    helper.setFrom(fromAddress);    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);    try {        mailSender.send(message);        System.out.println("发送成功!");    }catch (Exception e){        System.out.println("发送失败!");        System.out.println(e.getMessage());    }}

4、发送模板邮件

首先,我们需要在resources目录下的templates下创建一个emailTemplate.html文件,也就是我们的模板

    
邮件模板您好,感谢您的注册,这是一封验证邮件,请点击下面的链接完成注册,感谢您的支持!激活账号
  • 四、测试用例

1、发送文本邮件

@Testpublic void sendHtmlMail() throws MessagingException {    //编辑邮件内容    String content="Hello,我正在用SpringBoot给你发邮件,祝你生活愉快!";    mailService.sendSimpleMail(    		// 收件人地址            "xxxxxxxxxx@qq.com",            // 标题            "你好",            // 内容            content);}

2、发送Html邮件

@Testpublic void sendHtmlMail() throws MessagingException {    // 编辑HTML内容    String content="\n"+            "\n"+            "

Hello!这是一封SpringBoot发送的HTML邮件。

"+ "\n"+ ""; mailService.sendHtmlMail( "xxxxxxxxxx@qq.com", "你好", content);}

3、发送附件邮件

@Testpublic void sendAttachmentMail() throws MessagingException {	// 文件的路径    String filePath="H:/user/user.txt";    mailService.sendAttachmentMail(            "xxxxxxxxxx@qq.com",            "lizhou你好,这是一封带附件的内容",            "这是一封使用SpringBoot发送的带附件的邮件!",            filePath    );}

4、发送模板邮件

@Testpublic void TestTemplateMailTest() throws MessagingException {    Context context=new Context();    // 这里的id与html文件中的${id}必须对应    context.setVariable("id",006);    // 这里的"emailTemplate",必须是上面的html文件的文件名    String emailContent=templateEngine.process("emailTemplate",context);    mailService.sendHtmlMail(    		// 收件人地址            "xxxxxxxxxx@qq.com",            // 邮件标题            "这是一个模板邮件",            // 邮件正文            emailContent);}

至此,使用SpringBoot发送四种类型的邮件方法及测试用例都已经完成。

如您在阅读中发现不足,欢迎留言!!!

转载地址:https://lizhou.blog.csdn.net/article/details/98971321 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:JavaScript中监听鼠标滚轮事件
下一篇:MySQL中关于GROUP_CONCAT(expr)函数的使用

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月20日 11时53分41秒