平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TxySmsUtil.cs 1.5 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using SafeCampus.Core.Options;
  2. using TencentCloud.Common;
  3. using TencentCloud.Common.Profile;
  4. using TencentCloud.Sms.V20210111;
  5. using TencentCloud.Sms.V20210111.Models;
  6. namespace SafeCampus.Core.Utils.TXYSMS;
  7. public static class TxySmsUtil
  8. {
  9. /// <summary>
  10. /// 发送短信
  11. /// </summary>
  12. /// <param name="phone">new[] { $"+86{phone}" } 手机号码</param>
  13. /// <param name="content">new[] { code.ToString(), "5" } 内容参数</param>
  14. /// <returns></returns>
  15. public static bool SendSms(string[] phone, string[] content)
  16. {
  17. var setting = App.GetOptionsMonitor<TXSmsOptions>();
  18. Credential cred = new()
  19. {
  20. SecretId = setting.SecretId,
  21. SecretKey = setting.SecretKey
  22. };
  23. ClientProfile clientProfile = new();
  24. HttpProfile httpProfile = new()
  25. {
  26. Endpoint = ("sms.tencentcloudapi.com"),
  27. };
  28. clientProfile.HttpProfile = httpProfile;
  29. SmsClient client = new(cred, "ap-beijing", clientProfile);
  30. SendSmsRequest req = new()
  31. {
  32. PhoneNumberSet = phone,// new[] { $"+86{phone}" },
  33. SmsSdkAppId = setting.SmsAppId,
  34. SignName = setting.SmsSn,
  35. TemplateId = setting.VerificationCodeId,
  36. TemplateParamSet = content,
  37. };
  38. SendSmsResponse resp = client.SendSmsSync(req);
  39. if (resp.SendStatusSet[0].Code == "Ok")
  40. {
  41. // 缓存验证码
  42. return true;
  43. }
  44. return false;
  45. }
  46. }