欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot發(fā)送異步郵件流程與實(shí)現(xiàn)詳解

 更新時(shí)間:2024年01月04日 09:10:10   作者:Splaying  
這篇文章主要介紹了SpringBoot發(fā)送異步郵件流程與實(shí)現(xiàn)詳解,Servlet階段郵件發(fā)送非常的復(fù)雜,如果現(xiàn)代化的Java開發(fā)是那個(gè)樣子該有多糟糕,現(xiàn)在SpringBoot中集成好了郵件發(fā)送的東西,而且操作十分簡(jiǎn)單容易上手,需要的朋友可以參考下

1、基本簡(jiǎn)介

  • Servlet階段郵件發(fā)送非常的復(fù)雜,如果現(xiàn)代化的Java開發(fā)是那個(gè)樣子該有多糟糕;現(xiàn)在SpringBoot中集成好了郵件發(fā)送的東西,而且操作十分簡(jiǎn)單容易上手。
  • 發(fā)送郵件最主要的一步是需要去對(duì)應(yīng)的郵箱開啟POP3/SMTP 或者 IMAP/SMTP,并且拿到授權(quán)碼!
  • 導(dǎo)入依賴包spring-boot-starter-mail。
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2、郵件發(fā)送流程

首先導(dǎo)入mail的依賴包之后,根據(jù)SpringBoot的自動(dòng)裝配原理。幾乎所有的Bean對(duì)象都給裝配好了,直接拿來可以使用。

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)	
@EnableConfigurationProperties(MailProperties.class)			//配置文件綁定
@Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class })
public class MailSenderAutoConfiguration {

	static class MailSenderCondition extends AnyNestedCondition {

		MailSenderCondition() {
			super(ConfigurationPhase.PARSE_CONFIGURATION);
		}
		// 配置文件前綴名稱
		@ConditionalOnProperty(prefix = "spring.mail", name = "host")
		static class HostProperty {

		}

		@ConditionalOnProperty(prefix = "spring.mail", name = "jndi-name")
		static class JndiNameProperty {

		}
	}
}

簡(jiǎn)單分析一手可以看到只需要配置一下application文件即可,前綴以spring.mail開頭;

spring:
  mail:
    username: xxxx@163.com
    password: 授權(quán)碼
    host: smtp.163.com				# 不同郵箱對(duì)應(yīng)的服務(wù)器不一樣
    default-encoding: UTF-8
    
#    username: xxxx@qq.com
#    password: 授權(quán)碼
#    host: smtp.qq.com

3、配置線程池

這個(gè)步驟不是必須的,但是使用異步的郵件建議還是配置一下。

@Configuration
@EnableAsync//開啟異步
public class ThreadPoolConfig {

    @Bean("threadPool")
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 設(shè)置核心線程數(shù)
        executor.setCorePoolSize(5);
        // 設(shè)置最大線程數(shù)
        executor.setMaxPoolSize(10);
        // 設(shè)置隊(duì)列容量
        executor.setQueueCapacity(100);
        // 設(shè)置線程活躍時(shí)間(秒)
        executor.setKeepAliveSeconds(60);
        // 設(shè)置默認(rèn)線程名稱
        executor.setThreadNamePrefix("Thread-");
        // 設(shè)置拒絕策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任務(wù)結(jié)束后再關(guān)閉線程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }
}

4、編寫郵件工具類

  • 郵件在SpringBoot中被封裝成簡(jiǎn)單純文本郵件、多媒體郵件兩種形式。
  • 針對(duì)上述兩種形式的郵件可以進(jìn)行編寫一個(gè)工具類便于操作使用。
public class EmailUtil {

    private static final String content = "這是郵件的主內(nèi)容";
	
	// 純文本郵件
    public static SimpleMailMessage simple(String from, String to){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject("歡迎xxx");
        message.setText(content);
        return message;
    }

	// 多媒體郵件
    public static MimeMessage mimeMessage(MimeMessage message,String from, String to) throws MessagingException {
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject("復(fù)雜郵件主題");
        helper.setText("<p style='color: deepskyblue'>這是淺藍(lán)色的文字</p>", true);
        helper.addAttachment("1.docx", new File("C:\\Users\\Splay\\Desktop\\說明書.docx"));
        helper.addAttachment("1.pdf", new File("C:\\Users\\Splay\\Desktop\\參考.pdf"));
        return message;
    }
}

5、郵件發(fā)送

郵件發(fā)送是需要時(shí)間的,因此為了不讓用戶等待的時(shí)間過長建議這里使用異步的形式發(fā)送。避免阻塞

@Service
public class SendEmailService {

	// SpringBoot自動(dòng)裝配的郵件發(fā)送實(shí)現(xiàn)類
    @Autowired
    JavaMailSenderImpl mailSender;

    @Async("threadPool")
    public void sendSimpleMail(String from, String to) {
        SimpleMailMessage message = EmailUtil.simple(from, to);
        mailSender.send(message);                   //簡(jiǎn)單純文本郵件發(fā)送
    }

    @Async("threadPool")
    public void sendMultiPartMail(String from, String to) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessage mimeMessage = EmailUtil.mimeMessage(message,from, to);
        mailSender.send(mimeMessage);               //復(fù)雜郵件發(fā)送
    }

}

6、編寫controller

編寫幾個(gè)controller進(jìn)行測(cè)試

@Controller
public class RouterController {

    @Value("${spring.mail.username}")
    private String from;


    @Autowired
    SendEmailService mail;

    @RequestMapping("/simple")
    @ResponseBody
    public String simpleMail(@RequestParam(required = true,name = "mail",defaultValue = "xxx@qq.com") String to) {
        mail.sendSimpleMail(from, to);
        return "Send SimpleEmail Successful!";
    }

    @RequestMapping("/complex")
    @ResponseBody
    public String complexMail(@RequestParam(required = true,name = "mail",defaultValue = "xxx@qq.com") String to) throws MessagingException {
        mail.sendMultiPartMail(from, to);
        return "Send SimpleEmail Successful!";
    }
}

到此這篇關(guān)于SpringBoot發(fā)送異步郵件流程與實(shí)現(xiàn)詳解的文章就介紹到這了,更多相關(guān)SpringBoot發(fā)送異步郵件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一篇文章帶你搞定JAVA注解

    一篇文章帶你搞定JAVA注解

    這篇文章主要介紹了詳解Java注解的實(shí)現(xiàn)與使用方法的相關(guān)資料,希望通過本文大家能夠理解掌握J(rèn)ava注解的知識(shí),需要的朋友可以參考下
    2021-07-07
  • springboot?正確的在異步線程中使用request的示例代碼

    springboot?正確的在異步線程中使用request的示例代碼

    這篇文章主要介紹了springboot中如何正確的在異步線程中使用request,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • ?Spring?中?Bean?的生命周期詳解

    ?Spring?中?Bean?的生命周期詳解

    這篇文章主要介紹了Spring中Bean的生命周期詳解,Java中的公共類稱之為Bean或Java?Bean,而Spring中的Bean指的是將對(duì)象的生命周期
    2022-09-09
  • Java開發(fā)之內(nèi)部類對(duì)象的創(chuàng)建及hook機(jī)制分析

    Java開發(fā)之內(nèi)部類對(duì)象的創(chuàng)建及hook機(jī)制分析

    這篇文章主要介紹了Java開發(fā)之內(nèi)部類對(duì)象的創(chuàng)建及hook機(jī)制,結(jié)合實(shí)例形式分析了java基于hook機(jī)制內(nèi)部類對(duì)象的創(chuàng)建與使用,需要的朋友可以參考下
    2018-01-01
  • SpringBoot常見錯(cuò)誤圖文總結(jié)

    SpringBoot常見錯(cuò)誤圖文總結(jié)

    最近在使用idea+Springboot開發(fā)項(xiàng)目中遇到一些問題,這篇文章主要給大家介紹了關(guān)于SpringBoot常見錯(cuò)誤總結(jié)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • java代碼實(shí)現(xiàn)MD5加密及驗(yàn)證過程詳解

    java代碼實(shí)現(xiàn)MD5加密及驗(yàn)證過程詳解

    這篇文章主要介紹了java代碼實(shí)現(xiàn)MD5加密及驗(yàn)證過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Idea安裝bpmn插件actiBPM的詳細(xì)過程(解決高版本無法安裝actiBPM插件)

    Idea安裝bpmn插件actiBPM的詳細(xì)過程(解決高版本無法安裝actiBPM插件)

    這篇文章主要介紹了Idea安裝bpmn插件actiBPM的詳細(xì)過程(解決高版本無法安裝actiBPM插件)的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • 微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析

    微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析

    這篇文章主要為大家介紹了微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • 實(shí)例化JFileChooser對(duì)象報(bào)空指針異常問題的解決辦法

    實(shí)例化JFileChooser對(duì)象報(bào)空指針異常問題的解決辦法

    今天小編就為大家分享一篇關(guān)于實(shí)例化JFileChooser對(duì)象報(bào)空指針異常問題的解決辦法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 詳解spring cloud分布式整合zipkin的鏈路跟蹤

    詳解spring cloud分布式整合zipkin的鏈路跟蹤

    這篇文章主要介紹了詳解spring cloud分布式整合zipkin的鏈路跟蹤,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07

最新評(píng)論