12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using Newtonsoft.Json;
- using System;
- using System.Net.Http;
- using TencentCloud.Common;
- using TencentCloud.Common.Profile;
- using TencentCloud.Ses.V20201002;
- using TencentCloud.Ses.V20201002.Models;
- using XYY.Core.Standard.Data.Infrastructure;
- using XYY.Model.Standard.TencentCloud;
- namespace XYY.Service.Standard.TencentCloud
- {
- /// <summary>
- /// 腾讯云邮件发送服务
- /// </summary>
- public interface ITencentMailService
- {
- SendEmailResponse SendMail(SendMail sendMail);
- }
- public class TencentMailService : ITencentMailService
- {
- private readonly IUnitOfWork _unitOfWork;
- public TencentMailService(IUnitOfWork unitOfWork)
- {
- _unitOfWork = unitOfWork;
- }
- public SendEmailResponse SendMail(SendMail sendMail)
- {
- Credential cred = new Credential
- {
- SecretId = sendMail.SecretId,
- SecretKey = sendMail.SecretKey,
- };
- // 实例化一个client选项,可选的,没有特殊需求可以跳过
- ClientProfile clientProfile = new ClientProfile();
- // 实例化一个http选项,可选的,没有特殊需求可以跳过
- HttpProfile httpProfile = new HttpProfile();
- httpProfile.Endpoint = "ses.tencentcloudapi.com";
- clientProfile.HttpProfile = httpProfile;
- var handler = new HttpClientHandler
- {
- ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
- };
- // 使用自定义的HttpClientHandler创建HttpClient
- var httpClient = new HttpClient(handler);
- // 实例化要请求产品的client对象,clientProfile是可选的
- SesClient client = new SesClient(cred, "ap-guangzhou", clientProfile);
- client.HttpClient= httpClient;
- // 实例化一个请求对象,每个接口都会对应一个request对象
- SendEmailRequest req = JsonConvert.DeserializeObject<SendEmailRequest>(JsonConvert.SerializeObject(sendMail));
- string json = JsonConvert.SerializeObject(req);
- // 返回的resp是一个SendEmailResponse的实例,与请求对象对应
- SendEmailResponse resp = client.SendEmailSync(req);
- // 输出json格式的字符串回包
- return resp;
- }
- }
- }
|