ITencentMailService.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Net.Http;
  4. using TencentCloud.Common;
  5. using TencentCloud.Common.Profile;
  6. using TencentCloud.Ses.V20201002;
  7. using TencentCloud.Ses.V20201002.Models;
  8. using XYY.Core.Standard.Data.Infrastructure;
  9. using XYY.Model.Standard.TencentCloud;
  10. namespace XYY.Service.Standard.TencentCloud
  11. {
  12. /// <summary>
  13. /// 腾讯云邮件发送服务
  14. /// </summary>
  15. public interface ITencentMailService
  16. {
  17. SendEmailResponse SendMail(SendMail sendMail);
  18. }
  19. public class TencentMailService : ITencentMailService
  20. {
  21. private readonly IUnitOfWork _unitOfWork;
  22. public TencentMailService(IUnitOfWork unitOfWork)
  23. {
  24. _unitOfWork = unitOfWork;
  25. }
  26. public SendEmailResponse SendMail(SendMail sendMail)
  27. {
  28. Credential cred = new Credential
  29. {
  30. SecretId = sendMail.SecretId,
  31. SecretKey = sendMail.SecretKey,
  32. };
  33. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  34. ClientProfile clientProfile = new ClientProfile();
  35. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  36. HttpProfile httpProfile = new HttpProfile();
  37. httpProfile.Endpoint = "ses.tencentcloudapi.com";
  38. clientProfile.HttpProfile = httpProfile;
  39. var handler = new HttpClientHandler
  40. {
  41. ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
  42. };
  43. // 使用自定义的HttpClientHandler创建HttpClient
  44. var httpClient = new HttpClient(handler);
  45. // 实例化要请求产品的client对象,clientProfile是可选的
  46. SesClient client = new SesClient(cred, "ap-guangzhou", clientProfile);
  47. client.HttpClient= httpClient;
  48. // 实例化一个请求对象,每个接口都会对应一个request对象
  49. SendEmailRequest req = JsonConvert.DeserializeObject<SendEmailRequest>(JsonConvert.SerializeObject(sendMail));
  50. string json = JsonConvert.SerializeObject(req);
  51. // 返回的resp是一个SendEmailResponse的实例,与请求对象对应
  52. SendEmailResponse resp = client.SendEmailSync(req);
  53. // 输出json格式的字符串回包
  54. return resp;
  55. }
  56. }
  57. }