【WEEK15】 【DAY2】【DAY3】Email Tasks【English Version】

Continuation from【WEEK15】 【DAY1】Asynchronous Tasks【English Version】

Contents

  • 17. Asynchronous, Timed, and Email Tasks
    • 17.2. Email Tasks
      • 17.2.1. Email sending is also very common in our daily development, and Springboot provides support for this as well.
      • 17.2.2. Add spring-boot-starter-mail dependency
      • 17.2.3. View the auto-configuration class MailSenderAutoConfiguration.class
      • 17.2.4. Modify application.properties
        • 17.2.4.1. Get authorization code
      • 17.2.5. Unit Testing
        • 17.2.5.1. Modify Springboot09TestApplicationTests.java
        • 17.2.5.2. Run the project
      • 17.2.6. Modify Springboot09TestApplicationTests.java to send a relatively complex email
      • 17.2.7. Encapsulation (Complete code of Springboot09TestApplicationTests.java)

2024.6.4 Tuesday

17. Asynchronous, Timed, and Email Tasks

17.2. Email Tasks

17.2.1. Email sending is also very common in our daily development, and Springboot provides support for this as well.

  • Email sending requires introducing spring-boot-starter-mail
  • SpringBoot automatically configures MailSenderAutoConfiguration
  • Define MailProperties content and configure it in application.yml
  • Auto-configure JavaMailSender
  • Test email sending

17.2.2. Add spring-boot-starter-mail dependency

<!--Email Tasks-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>

17.2.3. View the auto-configuration class MailSenderAutoConfiguration.class

This class does not register a bean. Open MailSenderJndiConfiguration.class to see
Insert image description here

View the configuration file MailProperties.class
Insert image description here

/** Copyright 2012-2021 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.boot.autoconfigure.mail;import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;import org.springframework.boot.context.properties.ConfigurationProperties;/*** Configuration properties for email support.** @author Oliver Gierke* @author Stephane Nicoll* @author Eddú Meléndez* @since 1.2.0*/
@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;/*** SMTP server host. For instance, 'smtp.example.com'.*/private String host;/*** SMTP server port.*/private Integer port;/*** Login user of the SMTP server.*/private String username;/*** Login password of the SMTP server.*/private String password;/*** Protocol used by the SMTP server.*/private String protocol = "smtp";/*** Default MimeMessage encoding.*/private Charset defaultEncoding = DEFAULT_CHARSET;/*** Additional JavaMail Session properties.*/private Map<String, String> properties = new HashMap<>();/*** Session JNDI name. When set, takes precedence over other Session settings.*/private String jndiName;public String getHost() {return this.host;}public void setHost(String host) {this.host = host;}public Integer getPort() {return this.port;}public void setPort(Integer port) {this.port = port;}public String getUsername() {return this.username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return this.password;}public void setPassword(String password) {this.password = password;}public String getProtocol() {return this.protocol;}public void setProtocol(String protocol) {this.protocol = protocol;}public Charset getDefaultEncoding() {return this.defaultEncoding;}public void setDefaultEncoding(Charset defaultEncoding) {this.defaultEncoding = defaultEncoding;}public Map<String, String> getProperties() {return this.properties;}public void setJndiName(String jndiName) {this.jndiName = jndiName;}public String getJndiName() {return this.jndiName;}}

17.2.4. Modify application.properties

spring.application.name=springboot-09-test
spring.mail.username=email account
spring.mail.password=password
spring.mail.host=smtp.qq.com
# qq needs to configure SSL (enable password verification)
spring.mail.properties.mail.smtp.ssl.enable=true

Insert image description here

17.2.4.1. Get authorization code

https://service.mail.qq.com/detail/0/141
Insert image description here
Insert image description here

17.2.5. Unit Testing

17.2.5.1. Modify Springboot09TestApplicationTests.java

Insert image description here

package com.P51;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;@SpringBootTest
class Springboot09TestApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testvoid contextLoads() {// A simple emailSimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject("Email Subject Here");mailMessage.setText("This is the body of the email");mailMessage.setTo("xxx");mailMessage.setFrom("xxx");mailSender.send(mailMessage);}}
17.2.5.2. Run the project

Insert image description here
2024.6.5 Wednesday

17.2.6. Modify Springboot09TestApplicationTests.java to send a relatively complex email

package com.P51;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;@SpringBootTest
class Springboot09TestApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testvoid contextLoads() {// A simple emailSimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject("Email Subject Here");mailMessage.setText("This is the body of the email");mailMessage.setTo("xxx"); // RecipientmailMessage.setFrom("xxx");  // SendermailSender.send(mailMessage);}@Testvoid contextLoads1() throws MessagingException {// A complex emailMimeMessage mimeMessage = mailSender.createMimeMessage();// AssemblyMimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);// Contenthelper.setSubject("Email Subject for contextLoads1 Here");helper.setText("<p style='color:red'>This is the body of the contextLoads1 email</p>", true);// Attachmentshelper.addAttachment("Content_to_configure_after_creating_a_new_springboot_project.pdf", new File("Physical address of the file on the computer"));helper.addAttachment("running.ico", new File("Physical address of the ico on the computer"));helper.setTo("xxx");  // Recipienthelper.setFrom("xxx");   // SendermailSender.send(mimeMessage);}}

Insert image description here

17.2.7. Encapsulation (Complete code of Springboot09TestApplicationTests.java)

package com.P51;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;@SpringBootTest
class Springboot09TestApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testvoid contextLoads() {// A simple emailSimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject("Email Subject Here");mailMessage.setText("This is the body of the email");mailMessage.setTo("xxx"); // RecipientmailMessage.setFrom("xxx");  // SendermailSender.send(mailMessage);}@Testvoid contextLoads1() throws MessagingException {// A complex emailMimeMessage mimeMessage = mailSender.createMimeMessage();// AssemblyMimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);   // Supports multi-part uploads// Contenthelper.setSubject("Email Subject for contextLoads1 Here");helper.setText("<p style='color:red'>This is the body of the contextLoads1 email</p>", true);// Attachmentshelper.addAttachment("Content_to_configure_after_creating_a_new_springboot_project.pdf", new File("Physical address of the file on the computer"));helper.addAttachment("running.ico", new File("Physical address of the ico on the computer"));helper.setTo("xxx");  // Recipienthelper.setFrom("xxx");   // SendermailSender.send(mimeMessage);}// Encapsulation: Parameters can be encapsulated, not fully shown here/** Shortcut key for this comment: type /** and press Enter** @param html* @param subject* @param text* @throws MessagingException* @Author ZzzZzzzZzzzz-*/public void sendMail(Boolean html, String subject, String text) throws MessagingException {// A complex emailMimeMessage mimeMessage = mailSender.createMimeMessage();// AssemblyMimeMessageHelper helper = new MimeMessageHelper(mimeMessage, html);// Contenthelper.setSubject(subject);helper.setText(text, true);// Attachmentshelper.addAttachment("Content_to_configure_after_creating_a_new_springboot_project.pdf", new File("Physical address of the file on the computer"));helper.addAttachment("running.ico", new File("Physical address of the ico on the computer"));helper.setTo("xxx");  // Recipienthelper.setFrom("xxx");   // SendermailSender.send(mimeMessage);}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/23136.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/23136.shtml
英文地址,請注明出處:http://en.pswp.cn/web/23136.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

用戶的權限

一&#xff0c;用戶權限基礎知識 1&#xff0c;用戶的權限有&#xff1a; r&#xff1a;讀 w&#xff1a;寫 x&#xff1a;執行 2&#xff0c;文件的權限&#xff1a; r&#xff1a;可以執行cat、head、tail等命令讀取文件中的內容 w&#xff1a;可以用vi/vim或者重定向等…

JeecgBoot/SpringBoot升級Nacos(2.0.4到2.2.3)啟動報錯

錯誤如下&#xff1a; 報這種錯誤基本就很頭大了&#xff0c;是框架不兼容的問題&#xff0c;自己找很難找到解決方法。 解決方案是把SpringBoot框架版本調高。 修改前&#xff1a; <parent><groupId>org.springframework.boot</groupId><artifactId&g…

Dell戴爾XPS 16 9640 Intel酷睿Ultra9處理器筆記本電腦原裝出廠Windows11系統包,恢復原廠開箱狀態oem預裝系統

下載鏈接&#xff1a;https://pan.baidu.com/s/1j_sc8FW5x-ZreNrqvRhjmg?pwd5gk6 提取碼&#xff1a;5gk6 戴爾原裝系統自帶網卡、顯卡、聲卡、藍牙等所有硬件驅動、出廠主題壁紙、系統屬性專屬聯機支持標志、系統屬性專屬LOGO標志、Office辦公軟件、MyDell、邁克菲等預裝軟…

Linux基礎 (十四):socket網絡編程

我們用戶是處在應用層的&#xff0c;根據不同的場景和業務需求&#xff0c;傳輸層就要為我們應用層提供不同的傳輸協議&#xff0c;常見的就是TCP協議和UDP協議&#xff0c;二者各自有不同的特點&#xff0c;網絡中的數據的傳輸其實就是兩個進程間的通信&#xff0c;兩個進程在…

32C3-2模組與樂鑫ESP32--C3--WROOM--02模組原理圖、升級口說明

模組原理圖&#xff1a; 底板原理圖&#xff1a; u1 是AT通信口&#xff0c;wiif-tx wifi-rx 是升級口&#xff0c;chip-pu是reset復位口&#xff0c;GPIO9拉低復位進入下載模式 ESP32-WROOM-32 系列硬件連接管腳分配? 功能 ESP32 開發板/模組管腳 其它設備管腳 下載固件…

【Python報錯】AttributeError: ‘NoneType‘ object has no attribute ‘xxx‘

成功解決“AttributeError: ‘NoneType’ object has no attribute ‘xxx’”錯誤的全面指南 一、引言 在Python編程中&#xff0c;AttributeError是一種常見的異常類型&#xff0c;它通常表示嘗試訪問對象沒有的屬性或方法。而當我們看到錯誤消息“AttributeError: ‘NoneTyp…

激發AI創新潛能,OPENAIGC開發者大賽賽題解析

人工智能&#xff08;AI&#xff09;的飛速發展&#xff0c;特別是AIGC、大模型、數字人技術的成熟&#xff0c;不僅改變了數據處理和信息消費的方式&#xff0c;也為企業和個人提供了前所未有的機遇。在這種技術進步的背景下&#xff0c;由聯想拯救者、AIGC開放社區、英特爾共…

PostgreSQL的視圖pg_stat_database

PostgreSQL的視圖pg_stat_database pg_stat_database 是 PostgreSQL 中的一個系統視圖&#xff0c;用于提供與數據庫相關的統計信息。這個視圖包含了多個有用的指標&#xff0c;可以幫助數據庫管理員了解數據庫的使用情況和性能。 以下是 pg_stat_database 視圖的主要列和其含…

三生隨記——理發店詭事

在城市的邊緣&#xff0c;隱藏著一家不起眼的理發店。它沒有華麗的裝飾&#xff0c;也沒有喧囂的廣告&#xff0c;只是靜靜地矗立在一條狹窄的小巷盡頭。據說&#xff0c;這家店只在深夜營業&#xff0c;而且只接待那些真心尋求改變的人。 有一天&#xff0c;一個名叫林逸的年輕…

基于SSM+Jsp的高校二手交易平臺

開發語言&#xff1a;Java框架&#xff1a;ssm技術&#xff1a;JSPJDK版本&#xff1a;JDK1.8服務器&#xff1a;tomcat7數據庫&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;數據庫工具&#xff1a;Navicat11開發軟件&#xff1a;eclipse/myeclipse/ideaMaven包…

【遠程連接服務器】—— Workbench和Xshell遠程連接阿里云服務器失敗和運行Xshell報錯找不到 MSVCP110.d的問題分析及解決

提示&#xff1a;文章寫完后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 文章目錄 前言一、遠程連接不上服務器1. Workbench遠程連接失敗2.Xshell也連接不上3.解決方法(1)問題描述&#xff1a;(2)解決&#xff1a; 4.再次連接服務器 二、運行Xshell…

Android 上層的View透傳/不透傳 點擊事件 到下層

今天有個需求就是在本不該有laoding的地方加個 laoding&#xff0c;源碼中有騰訊的QMUI&#xff0c;所以選用了&#xff0c;QMUILoadingView。 但是有個問題&#xff0c;就是即使這個View蓋在最上層&#xff0c;顯示出來的時候&#xff0c;依然可以點擊下邊的控件。 處理&#…

【前端面試3+1】18 vue2和vue3父傳子通信的差別、props傳遞的數據在子組件是否可以修改、如何往window上添加自定義屬性、【多數元素】

一、vue2和vue3父傳子通信的差別 1、Vue2 父組件向子組件傳遞數據通常通過props屬性來實現。父組件可以在子組件的標簽中使用v-bind指令將數據傳遞給子組件的props屬性。在子組件中&#xff0c;可以通過props屬性來接收這些數據。這種方式是一種單向數據流的方式&#xff0c;父…

常用位算法

1&#xff0c;位翻轉 n^1 &#xff0c;n 是0 或 1&#xff0c;和 1 異或后位翻轉了。 2&#xff0c; 判斷奇偶&#xff0c;n&1&#xff0c;即判斷最后一位是0還是1&#xff0c;如果結果為0&#xff0c;就是偶數&#xff0c;是1 就是奇數。 獲取 32 位二進制的 1 的個數&a…

python-opencv圖像分割

文章目錄 二值化圖像骨骼連通域分割 二值化 所謂圖像分割&#xff0c;就是將圖像的目標和背景分離開來&#xff0c;更直觀一點&#xff0c;就是把目標涂成白色&#xff0c;背景涂成黑色&#xff0c;言盡于此&#xff0c;是不是恍然大悟&#xff1a;這不就是二值化么&#xff1…

香橙派 AIpro 的系統評測

0. 前言 你好&#xff0c;我是悅創。 今天受邀測評 Orange Pi AIpro開發板&#xff0c;我將準備用這個測試簡單的代碼來看看這塊開發版的性能體驗。 分別從&#xff1a;Sysbench、Stress-ng、PyPerformance、RPi.GPIO Benchmark、Geekbench 等方面來測試和分析結果。 下面就…

DevExpress Installed

一、What’s Installed 統一安裝程序將DevExpress控件和庫注冊到Visual Studio中&#xff0c;并安裝DevExpress實用工具、演示應用程序和IDE插件。 Visual Studio工具箱中的DevExpress控件 Visual Studio中的DevExpress菜單 Demo Applications 演示應用程序 Launch the Demo…

Python如何查詢數據庫:深入探索與實踐

Python如何查詢數據庫&#xff1a;深入探索與實踐 在數據驅動的世界中&#xff0c;Python作為一種強大且靈活的語言&#xff0c;自然成為了數據庫查詢的得力助手。本文將通過四個方面、五個方面、六個方面和七個方面&#xff0c;詳細探討Python如何查詢數據庫&#xff0c;并力…

elementary OS 8的新消息

原文&#xff1a;Happy Pride! Have Some Updates! ? elementary Blog 這個月&#xff0c;我們為OS 7帶來了一些意外驚喜&#xff0c;包括GNOME應用的新版本和郵件應用的重大更新。Wayland也來了&#xff0c;我們有了一種新的方式來管理驅動程序&#xff0c;并且我們現在默認…

PS去水印

去除圖片水印 step1&#xff1a;使用套索工具框選圖片水印 step2&#xff1a;CTRLshiftU 去色 step3&#xff1a;CTRLL 色階 step4&#xff1a;使用第三根吸管去點擊需要去掉的圖片水印 成功去掉 去掉文字水印 也可按照上述方法去除