SpringBoot集成E-mail發(fā)送各種類型郵件
SpringBoot 集成 E-mail發(fā)送郵件,供大家參考,具體內(nèi)容如下
JDK本身有自帶發(fā)送郵件api,加上SpringBoot在進(jìn)行封裝,使得現(xiàn)在使用起來(lái)十分快速簡(jiǎn)潔。
話不多說(shuō),參考純潔的微笑博客,更改jar版本為2.0.4 開干,基本沒什么坑。
就是配置郵箱賬號(hào)密碼是,如果是qq郵箱,需要開啟PO30和STMP服務(wù),并且獲取臨時(shí)授權(quán)碼。
開啟服務(wù)鏈接:
https://mail.qq.com/cgi-bin/frame_html?sid=a5ZSbreeNm9pHyl1&r=a83225170e94773c650a460c10f7a05c

與Springboot集成
導(dǎo)入jar包
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.0.4.RELEASE' compile 'org.springframework.boot:spring-boot-starter-thymeleaf:2.0.4.RELEASE'
配置郵箱
#郵箱服務(wù)器地址,各大運(yùn)營(yíng)商不同 spring.mail.host=smtp.qq.com #用戶名 spring.mail.username=9118542413@qq.com #密碼,如果是qq的,要申請(qǐng)臨時(shí)授權(quán)碼 spring.mail.password=faw124awfawfawg spring.mail.default-encoding=UTF-8 #以誰(shuí)來(lái)發(fā)送郵件 mail.fromMail.addr=9118542413@qq.com
發(fā)送各種類型的郵件
@Service
@Slf4j
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
/**
* The Template engine.
*/
@Autowired
TemplateEngine templateEngine;
@Value("${mail.fromMail.addr}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
log.info("簡(jiǎn)單郵件已經(jīng)發(fā)送。");
} catch (Exception e) {
log.error("發(fā)送簡(jiǎn)單郵件時(shí)發(fā)生異常!", e);
}
}
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要?jiǎng)?chuàng)建一個(gè)multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
log.info("html郵件發(fā)送成功");
} catch (MessagingException e) {
log.error("發(fā)送html郵件時(shí)發(fā)生異常!", e);
}
}
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
try {
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 = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
helper.addAttachment(fileName, file);
mailSender.send(message);
log.info("帶附件的郵件已經(jīng)發(fā)送。");
} catch (MessagingException e) {
log.error("發(fā)送帶附件的郵件時(shí)發(fā)生異常!", e);
}
}
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
log.info("嵌入靜態(tài)資源的郵件已經(jīng)發(fā)送。");
} catch (MessagingException e) {
log.error("發(fā)送嵌入靜態(tài)資源的郵件時(shí)發(fā)生異常!", e);
}
}
@Override
public void sendTemplateMail(String to, String subject, String template, Context context) {
String emailContent = templateEngine.process(template, context);
sendHtmlMail("15017263512@163.com", "主題:這是模板郵件", emailContent);
}
}
測(cè)試類:
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailServiceTest {
@Autowired
MailService mailService;
@Test
public void sendSimpleMailTest() {
mailService.sendSimpleMail("15017263512@163.com","test simple mail"," hello this is simple mail");
}
@Test
public void sendHtmlMailTest(){
String content="<html>\n" +
"<body>\n" +
" <h3>hello world ! 這是一封Html郵件!</h3>\n" +
"</body>\n" +
"</html>";
mailService.sendHtmlMail("15017263512@163.com","test html mail",content);
}
@Test
public void sendAttachmentsMail(){
String filePath="E:\\var\\log\\elkTest\\error\\2018-11-30.log";
mailService.sendAttachmentsMail("15017263512@163.com", "主題:帶附件的郵件", "有附件,請(qǐng)查收!", filePath);
}
@Test
public void sendInlineResourceMail() {
String rscId = "neo006";
String content="<html><body>這是有圖片的郵件:<img src=\'cid:" + rscId + "\' ></body></html>";
String imgPath = "C:\\Users\\Admin\\Pictures\\Camera Roll\\9499189867_1476052069.jpg";
mailService.sendInlineResourceMail("15017263512@163.com", "主題:這是有圖片的郵件", content, imgPath, rscId);
}
@Test
public void sendTemplateMail() {
//創(chuàng)建郵件正文
Context context = new Context();
context.setVariable("id", "006");
mailService.sendTemplateMail("15017263512@163.com","主題:這是模板郵件",
"emailTemplate",context);
}
}
上面的郵箱和密碼是我亂填的,注意自己更改。
項(xiàng)目源碼以上傳至GitHub
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java以struts2為例介紹如何實(shí)現(xiàn)圖片上傳
這篇文章主要介紹了Java struts2中如何實(shí)現(xiàn)圖片上傳的相關(guān)資料,需要的朋友可以參考下2015-11-11
Java實(shí)現(xiàn)Excel百萬(wàn)級(jí)數(shù)據(jù)導(dǎo)入功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)Excel百萬(wàn)級(jí)數(shù)據(jù)導(dǎo)入功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2024-04-04
IDEA創(chuàng)建Maven項(xiàng)目后報(bào)錯(cuò)不出現(xiàn)src文件夾的情況解決
最近剛開始學(xué)習(xí)maven,正準(zhǔn)備使用idea創(chuàng)建一個(gè)maven項(xiàng)目練手,卻發(fā)現(xiàn)自己創(chuàng)建的maven項(xiàng)目始終沒有src目錄,下面這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建Maven項(xiàng)目后報(bào)錯(cuò)不出現(xiàn)src文件夾的情況解決,需要的朋友可以參考下2023-05-05
一次 Java 服務(wù)性能優(yōu)化實(shí)例詳解
這篇文章主要介紹了一次 Java 服務(wù)性能優(yōu)化實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
在Java中如何避免創(chuàng)建不必要的對(duì)象
作為Java開發(fā)者,我們每天創(chuàng)建很多對(duì)象,但如何才能避免創(chuàng)建不必要的對(duì)象呢?這需要我們好好學(xué)習(xí),這篇文章主要給大家介紹了關(guān)于在Java中如何避免創(chuàng)建不必要對(duì)象的相關(guān)資料,需要的朋友可以參考下2021-10-10
Java設(shè)計(jì)模式之Template?Pattern模板模式詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之Template?Pattern模板模式詳解,模板模式(Template?Pattern)行為型模式之一,抽象父類定義一個(gè)操作中的算法的骨架,而將一些步驟延遲到子類中,需要的朋友可以參考下2023-10-10
Java與Python之間使用jython工具類實(shí)現(xiàn)數(shù)據(jù)交互
今天小編就為大家分享一篇關(guān)于Java與Python之間使用jython工具類實(shí)現(xiàn)數(shù)據(jù)交互,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03

