日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java技术:SpringBoot实现邮件发送功能

發布時間:2023/12/10 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java技术:SpringBoot实现邮件发送功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ?? ? ? ? ?

郵件發送功能基本是每個完整業務系統要集成的功能之一,今天小編給大家介紹一下SpringBoot實現郵件發送功能,希望對大家能有所幫助!

今天主要給大家分享簡單郵件發送、HTML郵件發送、包含附件的郵件發送三個例子,具體源碼鏈接在文章末尾,有需要的朋友可以自己下載學習一下。

1、創建一個基本的SpringBoot項目,pom文件導入發送郵件的依賴

<!--郵件發送依賴包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!--freemarker制作Html郵件模板依賴包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>

2、application.yml 文件配置配置郵件發送信息

spring: mail: host: smtp.qq.com username: xxx@qq.com #發件人郵箱 password: xxxxx #授權碼 protocol: smtp properties.mail.smtp.auth:true properties.mail.smtp.port:465#發件郵箱端口 properties.mail.display.sendmail: xiaoMing properties.mail.display.sendname: xiaoming properties.mail.smtp.starttls.enable:true properties.mail.smtp.starttls.required:true properties.mail.smtp.ssl.enable:true#是否啟用ssl default-encoding: utf-8#編碼格式 freemarker: cache:false settings: classic_compatible:true suffix: .html charset: UTF-8 template-loader-path: classpath:/templates/

3、創建IEmailService 接口文件,定義郵件發送的接口

package com.springboot.email.email.service;import javax.mail.MessagingException; import java.util.List;public interface IEmailService {/*** 發送簡單文本郵件*/void sendSimpleMail(String receiveEmail, String subject, String content);/*** 發送HTML格式的郵件*/void sendHtmlMail(String receiveEmail, String subject, String emailContent) throws MessagingException;/*** 發送包含附件的郵件*/void sendAttachmentsMail(String receiveEmail, String subject, String emailContent, List<String> filePathList) throws MessagingException; }

4、創建IEmailService接口的實現類EmailService.java 文件

package com.springboot.email.email.service.impl;import com.springboot.email.email.service.IEmailService; 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.annotation.Resource; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.List; @Service public class EmailServiceImpl implements IEmailService {@Resourceprivate JavaMailSender mailSender;@Value("${spring.mail.username}")private String fromEmail;/*** 發送簡單文本郵件*/public void sendSimpleMail(String receiveEmail, String subject, String content) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(fromEmail);message.setTo(receiveEmail);message.setSubject(subject);message.setText(content);mailSender.send(message);}/*** 發送Html格式的郵件*/public void sendHtmlMail(String receiveEmail,String subject,String emailContent) throws MessagingException{init(receiveEmail, subject, emailContent, mailSender, fromEmail);}public static void init(String receiveEmail, String subject, String emailContent, JavaMailSender mailSender, String fromEmail) throws MessagingException {MimeMessage message= mailSender.createMimeMessage();MimeMessageHelper helper=new MimeMessageHelper(message,true);helper.setFrom(fromEmail);helper.setTo(receiveEmail);helper.setSubject(subject);helper.setText(emailContent,true);mailSender.send(message);}/*** 發送包含附件的郵件*/public void sendAttachmentsMail(String receiveEmail, String subject, String emailContent, List<String> filePathList) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();//帶附件第二個參數trueMimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(fromEmail);helper.setTo(receiveEmail);helper.setSubject(subject);helper.setText(emailContent, true);//添加附件資源for (String item : filePathList) {FileSystemResource file = new FileSystemResource(new File(item));String fileName = item.substring(item.lastIndexOf(File.separator));helper.addAttachment(fileName, file);}//發送郵件mailSender.send(message);} }

5、新建郵件發送模板 email.html

<!DOCTYPE html> <html> <head lang="en"><meta charset="UTF-8"/><title></title><style>td {border: black 1px solid;}</style> </head> <body> <h1>工資條</h1> <table style="border: black 1px solid;width: 750px"><thead><td>序號</td><td>姓名</td><td>基本工資</td><td>在職天數</td><td>獎金</td><td>社保</td><td>個稅</td><td>實際工資</td></thead><tbody><tr><td>${salary.index}</td><td>${salary.name}</td><td>${salary.baseSalary}</td><td>${salary.inDays}</td><td>${salary.reward}</td><td>${salary.socialSecurity}</td><td>${salary.tax}</td><td>${salary.actSalary}</td></tr></tbody> </table> </body> </html>

6、新建測試類,主要代碼如下

/*** 測試簡單文本文件*/ @Test public void EmailTest() {emailService.sendSimpleMail("hgmyz@outlook.com", "測試郵件", "springboot 郵件測試"); }@Test public void HtmlEmailTest() throws MessagingException {String receiveEmail = "hgmyz@outlook.com";String subject = "Spring Boot 發送Html郵件測試";String emailContent = "<h2>您好!</h2><p>這里是一封Spring Boot 發送的郵件,祝您天天開心!<img " + "src='https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/a43f0608912a4ecfa182084e397e4b81?from=pc' width='500' height='300' /></p>" + "<a href='https://programmerblog.xyz' title='IT技術分享設社區' targer='_blank'>IT技術分享設社區</a>";emailService.sendHtmlMail(receiveEmail, subject, emailContent); }@Test public void templateEmailTest() throws IOException, TemplateException, MessagingException {String receiveEmail = "hgmyz@outlook.com";String subject = "Spring Boot 發送Templete郵件測試";//添加動態數據,替換模板里面的占位符SalaryVO salaryVO = new SalaryVO(1, "小明", 2, 9000.00, 350.06, 280.05, 350.00, 7806.00);Template template = freeMarkerConfigurer.getConfiguration().getTemplate("email.html");//將模板文件及數據渲染完成之后,轉換為html字符串Map<String, Object> model = new HashMap<>();model.put("salary", salaryVO);String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);emailService.sendHtmlMail(receiveEmail, subject, templateHtml); }@Test public void emailContailAttachmentTest() throws IOException, TemplateException, MessagingException {String receiveEmail = "hgmyz@outlook.com";String subject = "Spring Boot 發送包含附件的郵件測試";//添加動態數據,替換模板里面的占位符SalaryVO salaryVO = new SalaryVO(1, "小王", 2, 9000.00, 350.06, 280.05, 350.00, 7806.00);Template template = freeMarkerConfigurer.getConfiguration().getTemplate("email.html");//將模板文件及數據渲染完成之后,轉換為html字符串Map<String, Object> model = new HashMap<>();model.put("salary", salaryVO);String templateHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);List<String> fileList = new ArrayList<>();fileList.add("F:\\郵件測試.docx");fileList.add("F:\\5.png");fileList.add("F:\\db.jpg");emailService.sendAttachmentsMail(receiveEmail, subject, templateHtml, fileList); }

7、效果截圖

簡單文版郵件

? ? ? ? ? ? ? ?

html文件

? ? ? ? ? ? ? ?

包含附件的郵件

? ? ? ? ? ? ? ?

Gitee地址:https://gitee.com/hgm1989/springboot-email.git

IT技術分享社區

個人博客網站:https://programmerblog.xyz

文章推薦程序員效率:畫流程圖常用的工具程序員效率:整理常用的在線筆記軟件遠程辦公:常用的遠程協助軟件,你都知道嗎?51單片機程序下載、ISP及串口基礎知識硬件:斷路器、接觸器、繼電器基礎知識

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Java技术:SpringBoot实现邮件发送功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。