SpringBoot整合腾讯云邮件发送服务非STMP

news/2024/4/30 21:41:20

SpringBoot整合腾讯云邮箱服务

1、pom配置

<!--       腾讯云邮箱服务--><dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. --><!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 --><version>3.1.830</version></dependency>

2、yaml配置

tenxun:
#  secretId和secretKey来自于:https://console.cloud.tencent.com/cam/capisecretId: ""secretKey: ""
#  使用腾讯云的接口httpProfile: "ses.tencentcloudapi.com"
# 使用的服务器sesClient: "ap-hongkong"
# 发送邮箱的一些配置就是昵称,和发送域名fromEmailAddress : "云图柜 <kzzby@mail.kzzby.cn>"
# 配置的模板 id 按道理说这里是自定义的,但是由于本系统只能templateID : 120897

3、邮箱服务的模板配置

fromEmailAddress来自发信地址 - 邮件配置 - 邮件推送 - 控制台 (tencent.com)

image-20240329153740423

templateID来自

img

模板配置:

image-20240329153914744

这里使用的模板是

image-20240329153940409

html代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>验证码模板</title>
<style>body, html {height: 100%;margin: 0;display: flex;justify-content: center;align-items: center;background-color: #f0f0f0;font-family: Arial, sans-serif;}.verify-container {max-width: 600px;padding: 20px;border-radius: 10px;background-color: #ffffff;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);display: flex;flex-direction: column;align-items: center;text-align: center;gap: 10px;}.verify-description {font-size: 14px;color: #666;padding: 0 20px;}.verify-code {font-size: 24px;font-weight: bold;letter-spacing: 3px;color: #333;margin: 10px 0;padding: 10px 20px;border: 1px solid #ddd;border-radius: 5px;}.warning {font-size: 12px;color: #999;}
</style>
</head>
<body><div class="verify-container"><div class="verify-description">您好,您本次操作的验证码为:</div><span class="verify-code">{{Verify}}</span><div class="verify-description">请在5分钟内完成验证。请注意,验证码仅用于本次验证且不可重复使用。为保障账户安全,请勿泄露验证码。</div><div class="warning">超时或一次性使用后验证码将失效。感谢您的理解与配合。</div>
</div>
</body>
</html>

模板等待审核通过后即可使用

需要注意的是变量要变成这样{{Verify}}

  <span class="verify-code">{{Verify}}</span>

4、设置基础配置的bean

package com.hj.imgadmin.tenEml;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;@Configuration
@Data
public class TencentCloudConfig {@Value("${tenxun.secretId}")private String secretId;@Value("${tenxun.secretKey}")private String secretKey;@Value("${tenxun.httpProfile}")private String httpProfile;@Value("${tenxun.sesClient}")private String sesClient;@Value("${tenxun.fromEmailAddress}")private String fromEmailAddress;@Value("${tenxun.templateID}")private Long templateID;// 省略getter和setter方法
}

5、写服务方法

package com.hj.imgadmin.tenEml;import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ses.v20201002.SesClient;
import com.tencentcloudapi.ses.v20201002.models.SendEmailRequest;
import com.tencentcloudapi.ses.v20201002.models.SendEmailResponse;
import com.tencentcloudapi.ses.v20201002.models.Template;
import org.springframework.stereotype.Service;import javax.annotation.Resource;@Service
public class EmailService {@Resourceprivate  TencentCloudConfig config;/*** 发送验证码邮箱* @param subject 主题* @param receiverEml 接收者邮箱* @param verifyCode 验证码* @param templateID 这个自主修改,若采用更自由的配置方法那么就将这个变量加入其中*/public void sendVerifyCodeToEmail(String subject,String receiverEml, String verifyCode) {try {Credential cred = new Credential(config.getSecretId(), config.getSecretKey());HttpProfile httpProfile = new HttpProfile();httpProfile.setEndpoint(config.getHttpProfile());ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);SesClient client = new SesClient(cred, config.getSesClient(), clientProfile);SendEmailRequest req = new SendEmailRequest();req.setFromEmailAddress(config.getFromEmailAddress());req.setSubject(subject);String[] destination = {receiverEml};req.setDestination(destination);Template template = new Template();template.setTemplateID(120897L); // 替换为您的模板IDtemplate.setTemplateData("{\"Verify\":\""+verifyCode+"\"}");req.setTemplate(template);SendEmailResponse resp = client.SendEmail(req);System.out.println(resp);} catch (TencentCloudSDKException e) {System.out.println(e.toString());}}
}

6、测试

@Resource
private EmailService emailService;
@Test
void t34(){emailService.sendVerifyCodeToEmail("验证码","1245713658@qq.com","FcsRW");
}

控制台输出

com.tencentcloudapi.ses.v20201002.models.SendEmailResponse@4ba88ab5

邮箱显示

image-20240329162512473

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/11274.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

硬件15、PCB如何将元器件切换顶层和底层以及元器件布局对齐

器件切换顶层和底层 在移动元器件的时候&#xff0c;按一下 L 键&#xff0c;原器件就会变为蓝色&#xff0c;这就是放到了底层&#xff0c;然后再按 L 键就可以切换到顶层了 元器件放置后布局如何变得对齐那种 首先选中需要对齐的元器件&#xff0c;然后进行设置布局的属性…

vue3+threejs新手从零开发卡牌游戏(二十):添加卡牌被破坏进入墓地逻辑

在game目录下新建graveyard文件夹存放墓地相关代码&#xff1a; game/graveyard/p1.vue&#xff0c;这里主要设置了墓地group的位置&#xff1a; <template><div></div> </template><script setup lang"ts"> import { reactive, ref,…

Git常用指令使用

摘要&#xff1a;之前代码管理都是借助于fork、sourceTree等图形工具&#xff0c;最近发现直接用命令也好用&#xff0c;就总结Git常用的指令 1、Git的介绍 1.1 git官网 安装: Git - Downloading Packagehttps://git-scm.com/download/mac Mac上安装&#xff0c;直接使…

启扬RK3568核心板助力智慧步道轻装健身,打造全民健康生活新方式

随着物联网、AI智能等新技术的快速发展&#xff0c;智慧步道成为全国各地公园建设和全民健身公共服务设施改造的新主题。智慧步道基于物联网、人脸识别、大数据分析等技术&#xff0c;对人们的运动进行监测和数据采集&#xff0c;显示运动数据&#xff0c;包括里程统计、热量消…

钉钉服务端API报错 错误描述: robot 不存在;解决方案:请确认 robotCode 是否正确

problem 调用钉钉服务端API&#xff0c;机器人发送群聊消息&#xff0c;后台返回报错信息: 钉钉服务端API报错 错误描述: robot 不存在&#xff1b;解决方案:请确认 robotCode 是否正确&#xff1b; reason 定位: 登录后台&#xff0c;查看机器人是存在查看机器人调用权限接…

jupyter lab使用虚拟环境

python -m ipykernel install --name 虚拟环境名 --display-name 虚拟环境名然后再启动jupyter lab就行了