邮件任务|学习笔记

简介: 快速学习邮件任务

开发者学堂课程【SpringBoot 快速掌握 - 高级应用邮件任务】学习笔记,与课程紧密联系,让用户快速学习知识

课程地址https://developer.aliyun.com/learning/course/613/detail/9311


邮件任务

内容介绍

一.邮件任务

二.邮件发送


一.邮件任务

1.邮件发送需要引入 spring-boot-starter-mail

2.Spring Boot 自动配置 MailSenderAutoConfiguration

3.定义 MailProperties 内容,配置在 application.yml 中

4.自动装配 JavaMailSender

5.测试邮件发送


二.邮件发送

测试一下

Pom.xml 文件中,复制 <dependencies>

<dependency>

<grouprd>org-springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</ dependency>

场景启动器 start 中查询

复制 Spring-boot-starter-mail

引入

<dependency>

<grouprd>org-springframework.boot</groupId>

<artifactId>spring-boot-starter-mail</artifactId>

</ dependency>

邮件的自动配置,找到 mail 下 mailsenderautoconfiguration

打开后中有一个组件 javamailsendermail 即用于发送邮件。

邮件属性都可以在 mailproperties 中配置,属性有:

host,port,username,password 等等

邮件发送流程

image.png

配置属性

spring.mail.username=534096094@qq.com

spring.mail.password=gtstkoszjelabijb

spring.mai1.host=smtp.qq.com

测试

import org.junit.Test;

import org.junit.runner.Runwith;

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaNailsenderImpl;import org.springframework.test.context.junit4.SpringRunner;

@Runwith(SpringRunner.class)

@SpringBootTest

public class Springboot04TaskApplicationTests i

@Autowired

JavaMailsenderImpl mailSender;

Test

public void contextLoads( ) {

simpleMailMessage message = new SimpleMailMessage();/邮件设置

message. setsubject(“通知-今晚开会");

message.setText("今晚7:30开会");

message.setTo("175120806120163.com" ) ;

message.setFrom("534096094@qq.com“);

mailSender.send(message);

开始测试,报错,所以需要添加一些额外配置:

spring.mai1.properties.mail.smtp.ssl.enabLe=true

邮件发送成功。

2.复杂邮件的发送

@Test

public void teste2() throwsException{

//1、创建一个复杂的消息邮件

imeMessage mimeMessage = mailSender.createMimeMessage();

MimeMessageHelperhelper=new MimeMessageHelper(mimeMessage,multipart: true);

//邮件设置

helper.setSubject(“通知-今晚开会");

helper.setText("<b style='color:red' >今天7:30开会</b>");

helper.setTo("17512080612@163.com" ) ;

helper.setFrom( "5340960940qq.com");

//上传文件

Helper.addattchment(attachmentrilename:"1.jpg",new File(pathname:"c:\\users\\lfy\\Pictures\[|Saved Pictures\\1.jpg"))"

Helper.addattchment( attachmentFilename: "2.jpg" ,new File( pathname: "c: \|Users|lfyl\Pictures/|Saved Pictures\\2.jpg"))

mailSender.send(mimeMessage) ;

相关文章
|
2月前
|
搜索推荐 API 数据库
电子邮箱群发邮件工具?
电子邮箱群发邮件工具?
|
2月前
|
存储 弹性计算 运维
自动回复邮件脚本
【4月更文挑战第30天】
19 1
|
Android开发 iOS开发
Gmail中如何选择多封邮件
Gmail中如何选择多封邮件
|
监控 安全 开发者
安全日报邮件的发送和介绍|学习笔记
快速学习安全日报邮件的发送和介绍
108 0
安全日报邮件的发送和介绍|学习笔记
|
Java 开发者
实现发送带附件的邮件|学习笔记
快速学习实现发送带附件的邮件
204 0
实现发送带附件的邮件|学习笔记
|
网络协议 开发工具
小白都会的邮件推送?你还不会吗?
小白都会的邮件推送?你还不会吗?
406 1
小白都会的邮件推送?你还不会吗?
|
存储 网络协议 数据安全/隐私保护
基于163邮箱实现的邮件发送功能
基于163邮箱实现的邮件发送功能
436 0
基于163邮箱实现的邮件发送功能
|
域名解析 安全 搜索推荐