貿易網(wǎng)站建設sem投放是什么意思
使用SpringBoot發(fā)送郵件
郵件發(fā)送其實是一個非常常見的需求,用戶注冊,找回密碼等地方,都會用到,Spring Boot 中對于郵件發(fā)送,提供了相關的自動化配置類,使得郵件發(fā)送變得非常容易。
1、前置工作
目前國內大部分的郵件服務商都不允許直接使用用戶名/密碼的方式來在代碼中發(fā)送郵件,都是要先申請授權碼,這里以 QQ 郵箱為例,向大家演示授權碼的申請流程:
首先我們需要先登錄 QQ 郵箱網(wǎng)頁版,點擊上方的設置按鈕:然后點擊賬戶選項卡:在賬戶選項卡中找到開啟POP3/SMTP選項,如下:
點擊開啟,開啟相關功能,開啟過程需要手機號碼驗證,按照步驟操作即可,不贅述。開啟成功之后,即可獲取一個授權碼,將該號碼保存好,一會使用。
2、引入依賴、配置郵箱基本信息
<!--集成發(fā)送郵件的功能-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>
然后在yml配置文件中進行配置
spring:mail:host: smtp.qq.com # 設置郵箱主機port: 587 # SMTP 服務器的端口username: yyds@qq.com # 設置用戶名password: yyds # 設置密碼,該處的密碼是QQ郵箱開啟SMTP的授權碼而非QQ密碼mail:from: ${spring.mail.username}to: yyds@163.com
做完這些之后,Spring Boot 就會自動幫我們配置好郵件發(fā)送類,相關的配置在
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration
類中,部分源碼如下:
@Configuration
@ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)
@EnableConfigurationProperties(MailProperties.class)
@Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class })
public class MailSenderAutoConfiguration {}
可以看到,導入了另外一個配置 MailSenderPropertiesConfiguration
類,這個類中,提供了郵件發(fā)送相關的工具類,源碼如下:
@Configuration
@ConditionalOnProperty(prefix = "spring.mail", name = "host")
class MailSenderPropertiesConfiguration {private final MailProperties properties;MailSenderPropertiesConfiguration(MailProperties properties) {this.properties = properties;}@Bean@ConditionalOnMissingBeanpublic JavaMailSenderImpl mailSender() {JavaMailSenderImpl sender = new JavaMailSenderImpl();applyProperties(sender);return sender;}
}
可以看到,這里創(chuàng)建了一個 JavaMailSenderImpl
的實例, JavaMailSenderImpl
是 JavaMailSender
的一個實現(xiàn),我們將使用 JavaMailSenderImpl
來完成郵件的發(fā)送工作。
3、Service層代碼
自定義的MailProperties配置類,用于解析mail開頭的配置屬性
package com.yyds.domain;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "mail")
public class MailProperties {private String from;private String to;}
service層
package com.yyds.service;import freemarker.template.TemplateException;import javax.mail.MessagingException;
import java.io.IOException;
import java.util.Map;public interface MailService {void sendSimpleMail(String subject, String text) ;void sendHtmlMail(String subject, String text, Map<String, String> attachmentMap) throws MessagingException;void sendTemplateMail(String subject, Map<String, Object> params) throws MessagingException, IOException, TemplateException;
}
package com.yyds.service.impl;import com.yyds.domain.MailProperties;
import com.yyds.service.MailService;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Map;@Service
public class MailServiceImpl implements MailService {@Autowiredprivate JavaMailSender javaMailSender;@Autowiredprivate MailProperties myMailProperties;/*** 發(fā)送簡單文本郵件*/@Overridepublic void sendSimpleMail(String subject, String text) {SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setFrom(myMailProperties.getFrom());mailMessage.setTo(myMailProperties.getTo());mailMessage.setSubject(subject);mailMessage.setText(text);javaMailSender.send(mailMessage);}/*** 發(fā)送帶有鏈接和附件的復雜郵件*/@Overridepublic void sendHtmlMail(String subject, String text, Map<String, String> attachmentMap) throws MessagingException {MimeMessage mimeMessage = javaMailSender.createMimeMessage();//是否發(fā)送的郵件是富文本(附件,圖片,html等)MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);messageHelper.setFrom(myMailProperties.getFrom());messageHelper.setTo(myMailProperties.getTo());messageHelper.setSubject(subject);messageHelper.setText(text, true);//重點,默認為false,顯示原始html代碼,無效果if(attachmentMap != null){attachmentMap.entrySet().stream().forEach(entrySet -> {try {File file = new File(entrySet.getValue());if(file.exists()){messageHelper.addAttachment(entrySet.getKey(), new FileSystemResource(file));}} catch (MessagingException e) {e.printStackTrace();}});}javaMailSender.send(mimeMessage);}/*** 發(fā)送模版郵件*/@Overridepublic void sendTemplateMail(String subject, Map<String, Object> params) throws MessagingException, IOException, TemplateException {MimeMessage mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);helper.setFrom(myMailProperties.getFrom());helper.setTo(myMailProperties.getTo());freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_19);TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), "/templates/");configuration.setTemplateLoader(templateLoader);Template template = configuration.getTemplate("mail.ftl");String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);helper.setSubject(subject);helper.setText(html, true);//重點,默認為false,顯示原始html代碼,無效果javaMailSender.send(mimeMessage);}
}
4、發(fā)送郵件
4.1 測試發(fā)送簡單文本郵件
@SpringBootTest(classes = BootStartApplication.class)
public class MimeMailTest {@Autowiredprivate MailService mailService;@Testpublic void sendMail() {mailService.sendSimpleMail("測試Springboot發(fā)送郵件", "發(fā)送郵件...");}
}
4.2 測試發(fā)送帶有鏈接和附件的復雜郵件
package com.yyds;import com.yyds.service.MailService;
import freemarker.template.TemplateException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import javax.mail.MessagingException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;@SpringBootTest(classes = BootStartApplication.class)
public class MimeMailTest {@Autowiredprivate MailService mailService;@Testpublic void testMail() throws MessagingException {Map<String, String> attachmentMap = new HashMap<>();attachmentMap.put("附件", "D:\\D_ENL_MRO數(shù)據(jù)統(tǒng)計.xlsx");mailService.sendHtmlMail("測試Springboot發(fā)送帶附件的郵件2", "歡迎進入<a href=\"http://www.baidu.com\">百度首頁</a>", attachmentMap);}}
4.3 測試發(fā)送發(fā)送模版郵件
首先需要引入 Freemarker 依賴:
<!--整合freemarker--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>
然后在 resources/templates
目錄下創(chuàng)建一個 mail.ftl
作為郵件發(fā)送模板:
<html><body><h3>你好, <span style="color: red;">${username}</span>, 這是一封模板郵件!</h3></body>
</html>
package com.yyds;import com.yyds.service.MailService;
import freemarker.template.TemplateException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import javax.mail.MessagingException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;@SpringBootTest(classes = BootStartApplication.class)
public class MimeMailTest {@Autowiredprivate MailService mailService;@Testpublic void testFreemarkerMail() throws MessagingException, IOException, TemplateException {Map<String, Object> params = new HashMap<>();params.put("username", "Tom");mailService.sendTemplateMail("測試Springboot發(fā)送模版郵件", params);}}