【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.mzph.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;使用第三根吸管去点击需要去掉的图片水印 成功去掉 去掉文字水印 也可按照上述方法去除