手拉手spring-boot-starter-mail实现发送QQ邮箱

举报
QGS 发表于 2024/03/09 18:44:47 2024/03/09
【摘要】 手拉手spring-boot-starter-mail实现发送QQ邮箱

 

技术栈 springboot+mybatis-plus+mysql
软件 版本
IDEA IntelliJ IDEA 2022.2.1
JDK 17
Spring Boot 3.1
mybatis-plus 3.5
spring-boot-starter-mail Springboot版本

spring boot对mail的封装支持非常好,方便,几行代码就可以把邮件集成进来

spring-boot-starter-mail:

Spring框架提供了一个有用的实用程序库,用于发送电子邮件,使您免受底层邮件系统的限制,并负责代表客户端进行低级资源处理。

该org.springframework.mail软件包是Spring框架的电子邮件支持的根级软件包。用于发送电子邮件的中央界面是该MailSender 界面。封装了简单邮件(例如from和to,以及许多其他邮件)的属性的简单值对象是SimpleMailMessage类。此程序包还包含一个已检查异常的层次结构,该层次结构提供了比较低级别的邮件系统异常更高的抽象级别,根异常为 MailException。

一、QQ邮箱开通开通第三方登入服务

QQ邮箱开通第三方登入服务

​编辑

POP3/IMAP/SMTP/Exchange/CardDAV 服务已开启

在第三方客户端登录时,密码框请输入以下授权码:

​编辑

xxxxxxxxxxxxxxxx

pom.xml加入依赖

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

application.yml配置

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mysql?createDatabaseIfNotExist=true&autoReconnect=true&default-character-set=utf8&&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
 
  mail:
 # 下面这个是QQ邮箱host , 企业邮箱 smtp.exmail.qq.com
 host: smtp.qq.com
 # tencent mail port  这个是固定的
 port: 465
 # 你的QQ邮箱
 username: xxxxxxxqq.com
 # 进入邮箱配置后得到的授权码
 password: xxxxxxxxxxx
    test-connection: true
    properties:
      mail:
        smtp:
          ssl:
            enable: true


mail:

# 下面这个是QQ邮箱host , 企业邮箱 smtp.exmail.qq.com

host: smtp.qq.com

# tencent mail port 这个是固定的

port: 465

# 你的QQ邮箱

username: xxxxxxxqq.com

# 进入邮箱配置后得到的授权码

password: xxxxxxxxxxx

test-connection: true

properties:

mail:

smtp:

ssl:

enable: true

​编辑

编写Mail工具类

​编辑

package com.example.util;


import jakarta.mail.MessagingException;

import jakarta.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;

import org.springframework.mail.javamail.MimeMessageHelper;

import org.springframework.stereotype.Component;



import java.io.File;

@Component

public class SendMailUtils {

@Autowired

JavaMailSenderImpl javaMailSender;


//发送普通文字邮件

public void sendText(String Subject, String Text, String setFrom, String setTo) {

SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

simpleMailMessage.setSubject(Subject);//标题

simpleMailMessage.setText(Text);//内容

simpleMailMessage.setFrom(setFrom);//发送人邮箱

simpleMailMessage.setTo(setTo);//发送目的地邮箱

javaMailSender.send(simpleMailMessage);

}


//发送带页面格式加文件邮件

public void sendTexts(String Subject, String Text,Boolean t, String setFrom, String setTo

,String attachmentFilename,String filePathName) throws MessagingException {

MimeMessage mimeMessage=javaMailSender.createMimeMessage();

MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);

helper.setSubject(Subject);//标题

helper.setText(Text,t);//内容

helper.setFrom(setFrom);//发送人邮箱

helper.setTo(setTo);//目的地邮箱

helper.addAttachment(attachmentFilename,new File(filePathName)); //图片路径

javaMailSender.send(mimeMessage);

}



}

controller调用SendMailUtils工具类

@Autowired

SendMailUtils sendMailUtils;


@GetMapping("/sendMail")

public String sendMail(){

if (msg.isEmpty()){

String msg ="无告警信息";

}

sendMailUtils.sendText("Cpu使用率",msg,

" xxxx@qq.com","xxxx@qq.com");

return "ok";

}


@GetMapping("/sendMails")

public String sendMails(){

try {

sendMailUtils.sendTexts("发送带页面格式加文件邮件测试","<p style='color:red;'>红</p>",true, " xxxx@qq.com",

" xxxx@qq.com","redis图标","C:\\Users\\Administrator\\Desktop\\redis.png");

} catch (MessagingException e) {

throw new RuntimeException(e);

}

return "ok";

}

发送效果

​编辑

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。