<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>
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("xqlee注册"); // 返回的resp是一个SendEmailResponse的实例,与请求对象对应 SendEmailResponse resp = client.SendEmail(req); // 输出json格式的字符串回包 System.out.println(SendEmailResponse.toJsonString(resp)); } catch (TencentCloudSDKException e) { System.out.println(e.toString()); } } }
SecretId
, SecretKey
上面这两个参数从腾讯云https://console.cloud.tencent.com/cam/capi获取
no-replay@mail.your.com
发件人地址,在腾讯云SES后台配置的发信地址
jieshou@qq.com 收件人地址,这个就是你要发给目标的邮件地址
setTemplateID
模板id 腾讯SES后台模板管理里面通过审核的模板id
setTemplateData
模板参数,json字符串,替换模板里面的参数
http://blog.xqlee.com/article/1674231096096591873.html