目录
1、pom配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、yaml配置
spring:
mail:
# 比如:使用qq邮箱服务(也可以其他服务)
host: smtp.qq.com
#发送方的邮箱
username: xxx@qq.com
# 设置密码,该处的密码是发送方QQ邮箱开启SMTP的授权码而非QQ密码
password: xxxx
default-encoding: UTF-8
properties:
mail:
smtp:
ssl:
enable: true
其他邮箱服务:
smtp.163.com
:163邮箱smtp.126.com
:126邮箱smtp.qq.com
:qq邮箱
3、发送普通文本
代码:
Cntroller:
@RestController @RequestMapping("/mail") public class Controller { @Resource private MailService mailService; /** * 发送方邮件 * * @return */ @Value("${spring.mail.username}") private String sender; /** * 发送文本邮件 * * @return */ @GetMapping("/sendText") public String sendText() { //这个是接受人的邮箱 String receiver = "xxx@qq.com"; //标题 String subject = "邮箱标题(主题)"; //内容 String text = "这个是邮箱内容"; //文本邮箱 return mailService.sendTestMail(sender, receiver, subject, text); } }
Service:
@Service public class MailService { @Resource private JavaMailSender jms; public String sendTestMail(String sender, String receiver, String subject, String text) { //建立邮件消息 SimpleMailMessage mainMessage = new SimpleMailMessage(); //发送方 mainMessage.setFrom(sender); //接收方 mainMessage.setTo(receiver); //发送的标题 mainMessage.setSubject(subject); //发送的内容 mainMessage.setText(text); //发送邮件 jms.send(mainMessage); return "success"; } }
浏览器访问:http://localhost:8080/mail/sendText
查看邮件,内容如下:
4、发送html模板邮箱
代码:
Cntroller:
@RestController @RequestMapping("/mail") public class Controller { @Resource private MailService mailService; /** * 发送方邮件 * * @return */ @Value("${spring.mail.username}") private String sender; /** * html模板邮箱 * * @return */ @GetMapping("/sendHtml") public String sendHtml() { //这个是接受人的邮箱 String receiver = "xxx@qq.com"; //标题 String subject = "邮箱标题(主题)"; //内容 String content = "<html>\n"+ "<body>\n"+ "<h2>这是一封有历史意义的HTML邮件,请注意查收!!!</h2>\n"+ "</body>\n"+ "</html>"; //模板邮箱 return mailService.sendHtml(sender, receiver, subject, content); } }
Service:
@Service public class MailService { @Resource private JavaMailSender jms; public String sendHtml(String sender, String receiver, String subject, String content) { MimeMessage message = jms.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); //发送方 helper.setFrom(sender); //接收方 helper.setTo(receiver); //邮件主题 helper.setSubject(subject); //邮件内容 helper.setText(content, true); jms.send(message); } catch (MessagingException e) { e.printStackTrace(); } return "sendHtml success"; } }
浏览器访问: http://localhost:8080/mail/sendHtml
查看邮件,内容如下:
5、发送附件
代码:
Cntroller:
@RestController @RequestMapping("/mail") public class Controller { @Resource private MailService mailService; /** * 发送方邮件 * * @return */ @Value("${spring.mail.username}") private String sender; /** * 附件邮箱 * * @return */ @GetMapping("/sendAttachment") public String sendAttachment() { //这个是接受人的邮箱 String receiver = "xxx@qq.com"; //标题 String subject = "邮箱标题(主题)"; // 附件位置 String filePath = "C:\\Users\\Desktop\\Java整合Mail.txt"; String content = "我的这封邮件可以带附件!!!"; //附件邮箱 return mailService.sendAttachment(sender, receiver, subject, content, filePath); } }
Service:
@Service public class MailService { @Resource private JavaMailSender jms; public String sendAttachment(String sender, String receiver, String subject, String content, String filePath) { MimeMessage message = jms.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); //发送方 helper.setFrom(sender); //接收方 helper.setTo(receiver); //邮件主题 helper.setSubject(subject); //邮件内容 helper.setText(content, true); //邮件的附件 FileSystemResource file = new FileSystemResource(new File(filePath)); //获取附件的文件名 String fileName = file.getFilename(); helper.addAttachment(fileName, file); jms.send(message); } catch (MessagingException e) { e.printStackTrace(); } return "sendAttachment success"; } }
浏览器访问: http://localhost:8080/mail/sendAttachment
查看邮件,内容如下:
6、发送图片
代码:
Cntroller:
@RestController @RequestMapping("/mail") public class Controller { @Resource private MailService mailService; /** * 发送方邮件 * * @return */ @Value("${spring.mail.username}") private String sender; /** * 图片邮箱 * * @return */ @GetMapping("/sendPicture") public String sendPicture() { //这个是接受人的邮箱 String receiver = "xxx@qq.com"; //标题 String subject = "邮箱标题(主题)"; String picPath = "C:\\Users\\LiGezZ\\Desktop\\test.jpeg"; String picId = "PIC001"; String content = "<html><body>图片邮件\n" + "<img src=\'cid:" + picId + "\'></img></body></html>"; //图片邮箱 return mailService.sendPicture(sender, receiver, subject, content, picPath, picId); } }
Service:
@Service public class MailService { @Resource private JavaMailSender jms; public String sendPicture(String sender, String receiver, String subject, String content, String picPath, String picId) { MimeMessage message = jms.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); //发送方 helper.setFrom(sender); //接收方 helper.setTo(receiver); //邮件主题 helper.setSubject(subject); //邮件内容 helper.setText(content, true); //邮件的图片 FileSystemResource file = new FileSystemResource(new File(picPath)); helper.addInline(picId, file); jms.send(message); } catch (MessagingException e) { e.printStackTrace(); } return "sendPicture success"; } }
浏览器访问: http://localhost:8080/mail/sendPicture
查看邮件,内容如下:
7、结合模板引擎发送html邮箱
该方式时,引入thymeleaf
依赖,事先定义好html样式,html里面的参数是动态加载
的,由后端进行控制。与传统的html邮箱相比:可以定义更加复杂的样式、更加灵活的参数显示。
7.1添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
7.2 配置html页面
在resource/templates
下创建mail.html
,代码如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
<h1>用户列表</h1>
<table th:border="1" th:width="200px">
<tr>
<td>用户名</td>
<td>昵称</td>
<td>年龄</td>
</tr>
<tr th:each="user:${userList}">
<td th:width="40%" th:text="${user.username}" th:style="'background-color:'+(${user.odd}?'#fff':'#f5f2eb')"></td>
<td th:width="30%" th:text="${user.nickname}" th:style="'background-color:'+(${user.odd}?'#fff':'#f5f2eb')"></td>
<td th:width="30%" th:text="${user.age}" th:style="'background-color:'+(${user.odd}?'#fff':'#f5f2eb')"></td>
</tr>
</table>
</div>
</body>
</html>
7.3 Java代码
代码:
User实体:
public class User { private String username; private String nickname; private Integer age; private boolean odd; // 省略get、set }
Cntroller:
@RestController @RequestMapping("/mail") public class Controller { @Resource private MailService mailService; /** * 发送方邮件 * * @return */ @Value("${spring.mail.username}") private String sender; /** * Thymeleaf+html模板邮箱 * * @return */ @GetMapping("/sendThymeleaf") public String sendThymeleaf() { //这个是接受人的邮箱 String receiver = "xxx@qq.com"; //标题 String subject = "邮箱标题(主题)"; // Thymeleaf+html模板邮箱 return mailService.sendThymeleaf(sender, receiver, subject); } }
Service:
@Service public class MailService { @Resource private JavaMailSender jms; @Resource private TemplateEngine templateEngine; public String sendThymeleaf(String sender, String receiver, String subject) { try { //操作获取html模板 Context context = new Context(); // 设置html中的变量 List<User> userList = new ArrayList<>(); for (int i = 1; i <= 10; i++) { User user = new User(); user.setUsername("admin-" + i); user.setNickname("用户-" + i); user.setAge(20 + i); user.setOdd(i % 2 == 0); userList.add(user); } // 设置html参数 context.setVariable("userList", userList); //获取指定的html模板 String result = templateEngine.process("mail", context); //创建一个发送复杂消息对象 MimeMessage mimeMessage = jms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage); helper.setSubject(subject); //邮件内容,setText()第二个参数表示是否开启解析html helper.setText(result, true); helper.setFrom(sender); helper.setTo(receiver); jms.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } return "sendThymeleaf success"; } }
浏览器访问: http://localhost:8080/mail/sendThymeleaf
查看邮件,内容如下:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/18082.html