【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,一经查实,立即删除!

相关文章

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开放社区、英特尔共…

基于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…

【前端面试3+1】18 vue2和vue3父传子通信的差别、props传递的数据在子组件是否可以修改、如何往window上添加自定义属性、【多数元素】

一、vue2和vue3父传子通信的差别 1、Vue2 父组件向子组件传递数据通常通过props属性来实现。父组件可以在子组件的标签中使用v-bind指令将数据传递给子组件的props属性。在子组件中&#xff0c;可以通过props属性来接收这些数据。这种方式是一种单向数据流的方式&#xff0c;父…

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…

PS去水印

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

计算机网络 期末复习(谢希仁版本)第1章

大众熟知的三大网络&#xff1a;电信网络、有线电视网络、计算机网络。发展最快起到核心的是计算机网络。Internet是全球最大、最重要的计算机网络。互联网&#xff1a;流行最广、事实上的标准译名。互连网&#xff1a;把许多网络通过一些路由器连接在一起。与网络相连的计算机…

【多模态】35、TinyLLaVA | 3.1B 的 LMM 模型就可以实现 7B LMM 模型的效果

文章目录 一、背景二、方法2.1 模型结构2.2 训练 pipeline 三、模型设置3.1 模型结构3.2 训练数据3.3 训练策略3.4 评测 benchmark 四、效果 论文&#xff1a;TinyLLaVA: A Framework of Small-scale Large Multimodal Models 代码&#xff1a;https://github.com/TinyLLaVA/T…

【Unity性能优化】使用多边形碰撞器网格太多,性能消耗太大了怎么办

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 专栏交流&#x1f9e7;&…

【机器学习基础】Python编程04:五个实用练习题的解析与总结

Python是一种广泛使用的高级编程语言,它在机器学习领域中的重要性主要体现在以下几个方面: 简洁易学:Python语法简洁清晰,易于学习,使得初学者能够快速上手机器学习项目。 丰富的库支持:Python拥有大量的机器学习库,如scikit-learn、TensorFlow、Keras和PyTorch等,这些…

快团团有货源的供货大团长如何给单个订单发货?

快团团团长给单个订单发货的步骤如下&#xff1a; 登录快团团商家后台&#xff1a;首先&#xff0c;你需要以团长的身份登录快团团的商家后台管理系统。 进入订单管理页面&#xff1a;登录后&#xff0c;在后台导航中找到并点击“订单管理”或类似的选项&#xff0c;进入订单列…

算法人生(19): 从“LangChain的六大组件”看“个人职业规划”

我们今天要说说和大模型有着密切关系的Langchain &#xff0c;它提供了一个平台&#xff0c;让开发者可以更加轻松地训练、部署和管理这些大模型。具体来说&#xff0c;Langchain 可以通过提供高性能的计算资源、灵活的模型管理和部署选项、以及丰富的监控和调试功能&#xff0…

企业软件产品和服务 之 设计保证安全 七项承诺

1. 引言 公司如何保护自己免受数据泄露的影响&#xff1f;标准答案就是&#xff1a; “启用多因素身份验证”——MTA&#xff08;Enable multifactor authentication&#xff09;。 但是&#xff0c;目前很多公司仍然盲目地只使用密码作为唯一的身份来源。 网络安全的核心是…