Springboot 自动发送邮件
2023/11/23 18:03:08
本文主要是介绍Springboot 自动发送邮件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
完成Springboot配置发件邮箱,自动给其他邮箱发送邮件功能
一、创建springboot基础项目,引入依赖
<!-- Spring Boot 邮件依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- Spring Boot 模板依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
二、配置文件添加必要的配置:
#qq邮箱为例
spring.mail.host=smtp.qq.com spring.mail.port=465 spring.mail.username=89666XXX@qq.com spring.mail.password=XXXXXX #不是邮件密码而是授权码(邮箱设置里获取) spring.mail.protocol=smtp spring.mail.test-connection=true spring.mail.default-encoding=UTF-8 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.display.sendmail=spring-boot-demo
三、工程结构
四、MailService接口
package com.example.mail.demo; import javax.mail.MessagingException; public interface MailService { /** * 发送文本邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param cc 抄送地址 */ void sendSimpleMail(String to, String subject, String content, String... cc); /** * 发送HTML邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException; /** * 发送带附件的邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param filePath 附件地址 * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException; }
五、MailServiceImpl实现
package com.example.mail.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @Service public class MailServiceImpl implements MailService { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from; /** * 发送文本邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param cc 抄送地址 */ @Override public void sendSimpleMail(String to, String subject, String content, String... cc) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); if (cc != null) { message.setCc(cc); } mailSender.send(message); } /** * 发送HTML邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ @Override public void sendHtmlMail(String to, String subject, String content, String... cc) 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); if (cc != null) { helper.setCc(cc); } mailSender.send(message); } /** * 发送带附件的邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param filePath 附件地址 * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ @Override public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) 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); if (cc != null) { helper.setCc(cc); } FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = new File(filePath).getName(); System.out.println("fileName:"+fileName); helper.addAttachment(fileName, file); mailSender.send(message); } }
六、EmailController控制类
package com.example.mail.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import javax.mail.MessagingException; import java.io.File; import java.net.URL; @Controller public class EmailController { @Autowired private MailService mailService; /** * 测试发送简单邮件 */ @GetMapping("/testSendSimpleMail") @ResponseBody public String sendSimpleMail() { mailService.sendSimpleMail("2431886055@qq.com", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件"); return "ok"; } @Autowired private TemplateEngine templateEngine; /** * 发送html邮件 * @return * @throws MessagingException */ @GetMapping("/sendHtmlMail") @ResponseBody public String sendHtmlMail() throws MessagingException { Context context = new Context(); context.setVariable("project", "Spring Boot email"); context.setVariable("author", "万笑佛"); context.setVariable("url", "https://www.cnblogs.com/yclh/"); String emailTemplate = templateEngine.process("welcome", context); mailService.sendHtmlMail("2431886055@qq.com", "这是一封模板HTML邮件", emailTemplate); return "ok"; } /** * 发送带附件的邮件 * @throws MessagingException */ @GetMapping("/sendAttachmentsMail") @ResponseBody public String sendAttachmentsMail() throws MessagingException { URL url = null; try { // 创建一个文件对象 File file = new File("D:\\IDEANew\\45Mail\\src\\main\\resources\\static\\index.html"); // 将文件对象转换为URI url = file.toURI().toURL(); // 输出URL System.out.println("文件的URL: " + url); } catch (Exception e) { e.printStackTrace(); } System.out.println(url.getPath()); mailService.sendAttachmentsMail("2431886055@qq.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", url.getPath()); return "ok"; } }
七、postman测试
(1)第一个简单邮件
效果
(2)第二个带模板的邮件
效果
(3)第三个带附件的邮件
效果
这篇关于Springboot 自动发送邮件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-27JavaScript面试真题详解与解答
- 2024-12-27掌握JavaScript大厂面试真题:新手入门指南
- 2024-12-27JavaScript 大厂面试真题详解与解析
- 2024-12-26网络攻防资料入门教程
- 2024-12-26SQL注入资料详解:入门必读教程
- 2024-12-26初学者指南:数据库服务漏洞项目实战
- 2024-12-26网络安全项目实战:新手入门指南
- 2024-12-26网络攻防项目实战入门教程
- 2024-12-26信息安全项目实战:从入门到初步应用
- 2024-12-26SQL注入项目实战:初学者指南