中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

貿易網(wǎng)站建設sem投放是什么意思

貿易網(wǎng)站建設,sem投放是什么意思,wordpress 文章頁 下載地址,社群營銷平臺有哪些使用SpringBoot發(fā)送郵件 郵件發(fā)送其實是一個非常常見的需求,用戶注冊,找回密碼等地方,都會用到,Spring Boot 中對于郵件發(fā)送,提供了相關的自動化配置類,使得郵件發(fā)送變得非常容易。 1、前置工作 目前國內…

使用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 的實例, JavaMailSenderImplJavaMailSender 的一個實現(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);}}

在這里插入圖片描述

http://www.risenshineclean.com/news/65593.html

相關文章:

  • 網(wǎng)頁制作工具的選擇與網(wǎng)站整體風格網(wǎng)絡廣告推廣服務
  • 老司機做爰網(wǎng)站老師影音百度托管運營哪家好
  • 家裝設計師培訓學校湖南企業(yè)seo優(yōu)化推薦
  • 新手做網(wǎng)站做什么樣的網(wǎng)站建設公司哪個好呀
  • 怎么在微信做企業(yè)網(wǎng)站app開發(fā)費用標準
  • 有哪些設計的很優(yōu)秀的網(wǎng)站企業(yè)培訓內容
  • 沈陽智能模板建站軟文營銷范文100字
  • 武漢軟件100強企業(yè)名單北京官方seo搜索引擎優(yōu)化推薦
  • 資陽網(wǎng)站設計搜狗搜索網(wǎng)
  • 互聯(lián)網(wǎng)行業(yè) 英文郴州seo網(wǎng)絡優(yōu)化
  • 濟南做網(wǎng)站多少錢站長工具pr值查詢
  • 全免費云游戲網(wǎng)頁在線玩seo快速排名軟件價格
  • 東莞seo網(wǎng)站推廣個人怎么做免費百度推廣
  • 重慶網(wǎng)站建設有限公司拼多多運營
  • 網(wǎng)站運營模式有哪些短網(wǎng)址鏈接生成
  • 現(xiàn)在最靠譜的購物網(wǎng)站有哪些天津seo實戰(zhàn)培訓
  • 網(wǎng)站開發(fā)師培訓網(wǎng)絡營銷手段有哪四種
  • 深圳網(wǎng)站開發(fā)優(yōu)化營商環(huán)境 助推高質量發(fā)展
  • 建設一個網(wǎng)站需要提供什么手續(xù)seo網(wǎng)絡推廣專員
  • 十堰建設網(wǎng)站首頁無錫seo公司
  • 怎樣查看網(wǎng)站是用什么cms 做的企業(yè)培訓課程有哪些
  • 權重高的網(wǎng)站是幾vue seo優(yōu)化
  • 個人網(wǎng)站頁面設計需要那些步驟seo教學平臺
  • 北京朝陽不限購小戶型天津seo培訓
  • 住房和城鄉(xiāng)建設部網(wǎng)站辦事大廳各大免費推廣網(wǎng)站
  • 山東省住房和建設廳網(wǎng)站百度網(wǎng)址大全免費下載
  • 延安免費做網(wǎng)站怎么創(chuàng)作自己的網(wǎng)站
  • 設計師培訓學費長春seo優(yōu)化企業(yè)網(wǎng)絡躍升
  • 水墨畫風格網(wǎng)站網(wǎng)站快速排名互點軟件
  • 做兼職最好的網(wǎng)站杭州百度推廣開戶