邮件任务

邮件发送,在我们的日常开发中,也非常的多,Springboot也帮我们做了支持

  • 邮件发送需要引入spring-boot-start-mail
  • SpringBoot 自动配置MailSenderAutoConfiguration
  • 定义MailProperties内容,配置在application.yml中
  • 自动装配JavaMailSender
  • 测试邮件发送

测试:

1、引入pom依赖

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

看它引入的依赖,可以看到 jakarta.mail

<dependency>
   <groupId>com.sun.mail</groupId>
   <artifactId>jakarta.mail</artifactId>
   <version>1.6.4</version>
   <scope>compile</scope>
</dependency>

2、查看自动配置类:MailSenderAutoConfiguration
在这里插入图片描述
这个类中存在bean,JavaMailSenderImpl
在这里插入图片描述

然后我们去看下配置文件

@ConfigurationProperties(
   prefix = "spring.mail"
)
public class MailProperties {
   private static final Charset DEFAULT_CHARSET;
   private String host;
   private Integer port;
   private String username;
   private String password;
   private String protocol = "smtp";
   private Charset defaultEncoding;
   private Map<String, String> properties;
   private String jndiName;
}

3、配置文件:

spring.mail.username=24736743@qq.com
spring.mail.password=你的qq授权码
spring.mail.host=smtp.qq.com

# qq需要配置ssl
spring.mail.properties.mail.smtp.ssl.enable=true

获取授权码:在QQ邮箱中的设置->账户->开启pop3和smtp服务
在这里插入图片描述

4、Spring单元测试

@Autowired
JavaMailSenderImpl mailSender;

@Test
public void contextLoads() {
   //邮件设置1:一个简单的邮件
   SimpleMailMessage message = new SimpleMailMessage();
   message.setSubject("通知-明天来狂神这听课");
   message.setText("今晚7:30开会");

   message.setTo("24736743@qq.com");
   message.setFrom("24736743@qq.com");
   mailSender.send(message);
}

@Test
public void contextLoads2() throws MessagingException {
   //邮件设置2:一个复杂的邮件
   MimeMessage mimeMessage = mailSender.createMimeMessage();
   MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

   helper.setSubject("通知-明天来狂神这听课");
   helper.setText("<b style='color:red'>今天 7:30来开会</b>",true);

   //发送附件
   helper.addAttachment("1.jpg",new File(""));
   helper.addAttachment("2.jpg",new File(""));

   helper.setTo("24736743@qq.com");
   helper.setFrom("24736743@qq.com");

   mailSender.send(mimeMessage);
}

查看邮箱,邮件接收成功!

我们只需要使用Thymeleaf进行前后端结合即可开发自己网站邮件收发功能了!

狂神讲解的配套视频地址 https://www.bilibili.com/video/BV1PE411i7CV

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flowable中的邮件任务是一种特殊的服务任务,用于在业务流程中发送电子邮件。您可以通过设置服务任务的类型为"mail"来定义邮件任务邮件任务可以增强业务流程,包括支持抄送、密送、HTML内容等功能。需要注意的是,邮件任务不是BPMN 2.0规范的正式任务,因此在Flowable中,它被实现为一个专门的服务任务。 以下是一个示例,演示如何在Flowable中配置邮件任务: ```xml <serviceTask id="sendMail" flowable:type="mail"> <!-- 配置邮件服务器 --> <extensionElements> <flowable:field name="host"> <flowable:string><![CDATA[smtp.example.com]]></flowable:string> </flowable:field> <flowable:field name="port"> <flowable:string><![CDATA[587]]></flowable:string> </flowable:field> <flowable:field name="username"> <flowable:string><![CDATA[username@example.com]]></flowable:string> </flowable:field> <flowable:field name="password"> <flowable:string><![CDATA[password]]></flowable:string> </flowable:field> <flowable:field name="starttls"> <flowable:string><![CDATA[true]]></flowable:string> </flowable:field> </extensionElements> <!-- 配置邮件内容 --> <extensionElements> <flowable:field name="to"> <flowable:string><![CDATA[recipient@example.com]]></flowable:string> </flowable:field> <flowable:field name="subject"> <flowable:string><![CDATA[Hello]]></flowable:string> </flowable:field> <flowable:field name="text"> <flowable:string><![CDATA[This is the email body.]]></flowable:string> </flowable:field> </extensionElements> </serviceTask> ``` 在上述示例中,我们首先配置了邮件服务器的相关信息,包括主机、端口、用户名、密码和是否启用STARTTLS。然后,我们配置了邮件的收件人、主题和正文内容。 请注意,上述示例仅用于演示目的,实际使用时需要根据您的邮件服务器和业务需求进行相应的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值