腾讯云SES邮件推送服务Java实现

位置:首页>文章>详情   分类: 教程分享   阅读(511)   2024-03-11 09:36:32

环境说明

  • Java 1.8
  • maven项目

引入腾讯云ses依赖

<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java-common</artifactId>
    <version>3.1.787</version>
</dependency>
<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java-ses</artifactId>
    <version>3.1.787</version>
</dependency>

编写Java代码实现腾讯云SES发送邮件

package demo.tencent.cloud;

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;

public class MailTest {


        public static void main(String [] args) {
        try{
            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            Credential cred = new Credential("SecretId", "SecretKey");
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ses.tencentcloudapi.com");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SesClient client = new SesClient(cred, "ap-hongkong", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendEmailRequest req = new SendEmailRequest();
            /**
             * 发件人地址(腾讯ses配置里面的)
             */
            req.setFromEmailAddress("no-replay@mail.your.com");

            /**
             * 设置收信人邮件地址
             */
            String[] destination1 = {"jieshou@qq.com"};
            req.setDestination(destination1);

            Template template1 = new Template();
            template1.setTemplateID(12312L);
            template1.setTemplateData("{\"emailCode\":\"1231\"}");
            req.setTemplate(template1);

            req.setSubject("左搜注册");
            // 返回的resp是一个SendEmailResponse的实例,与请求对象对应
            SendEmailResponse resp = client.SendEmail(req);
            // 输出json格式的字符串回包
            System.out.println(SendEmailResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
}

 

代码参数说明

SecretIdSecretKey上面这两个参数从腾讯云https://console.cloud.tencent.com/cam/capi获取

no-replay@mail.your.com 发件人地址,在腾讯云SES后台配置的发信地址

jieshou@qq.com 收件人地址,这个就是你要发给目标的邮件地址

setTemplateID模板id  腾讯SES后台模板管理里面通过审核的模板id

setTemplateData模板参数,json字符串,替换模板里面的参数

 

地址:https://www.leftso.com/article/1674231096096591873.html

相关阅读

环境说明 Java 1.8maven项目 引入腾讯云ses依赖 &lt;dependency&gtl; &lt;groupId&gtl;com.tencentcloudapi&l...
腾讯云对象存储,配置微信小程序可以访问 如上图Referer第一行所示,添加该域名即可,微信小程序都是通过这个域名来访问的资源。
存储桶访问权限 存储桶访问权限分三种,分别是 私有读写; 公有读私有写; 公有读写; 理解: 私有读写:访问和编辑上传你存储的对象都需要验证权限; ...
前言最近有小伙伴买的腾讯云的1核1G入门级服务器,发现部署的服务多了后,会自动停掉一些docker的的容器
事情是这样的,昨天给博客进行了程序更新。刚开始都还是好好的。突然就访问错误了 经过排除发现是静态资源文件访问挂了,本站的静态资源一直是用的百度云加速的CDN服务。   于是马上去访问s...
nginx 配置文件编 cat static-vcnas-bind.example.conf server {
百度云网盘百度网盘介绍       百度网盘(原百度云)是百度推出的一项云存储服务,已覆盖主流PC和手机操作系统,包含Web版、Windows版、Mac版、Android版、iPhone版和Wi...
Java编程中发邮件也是常用的。但是原生的jdk自带的发送邮件用起来还是比较麻烦的。spring框架在Java语言中完全是神一样的存在,通过spring框架的邮件工具来发送邮件就非常方便了,本文...
前置说明,项目配置了个全局PATH,估计是为了方便复杂环境单容器多项目部署用的老项目配置方式var PATH = [[${#httpServletRequest.getScheme() + ":...
这里主要讲解如何通过调用api自动推送URL到bing站长平台,让Bing蜘蛛及时抓取你发布的内容。随着Windows 10 Windows11 全面推广后,自带的edge浏览器也越来越流行。默...