TencentHelper.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using SmsTool.Models;
  2. using System;
  3. using TencentCloud.Common;
  4. using TencentCloud.Common.Profile;
  5. using TencentCloud.Sms.V20210111;
  6. using TencentCloud.Sms.V20210111.Models;
  7. namespace SmsTool.Tencent
  8. {
  9. public class TencentHelper
  10. {
  11. private static SmsClient _client;
  12. private static string _smsSdkAppId = "1400537314";//应用id
  13. public static void Init()
  14. {
  15. var credential = new Credential
  16. {
  17. SecretId = "AKIDMSIsADUUpvJGwJOKvsb5IDrGtGNBRzzi",
  18. SecretKey = "4sQhdJBl5HI7f3oLbZIlwqXOyILyLWP0"
  19. };
  20. ClientProfile clientProfile = new ClientProfile();
  21. HttpProfile httpProfile = new HttpProfile();
  22. httpProfile.Endpoint = ("sms.tencentcloudapi.com");
  23. clientProfile.HttpProfile = httpProfile;
  24. _client = new SmsClient(credential, "ap-nanjing", clientProfile);
  25. }
  26. public static SendSmsResponse SendSmsSync(SendSmsModel model)
  27. {
  28. try
  29. {
  30. SendSmsResponse resp = _client.SendSmsSync(new SendSmsRequest
  31. {
  32. TemplateId = model.TemplateId,
  33. PhoneNumberSet = model.FormatterMobiles.ToArray(),
  34. TemplateParamSet = model.Params.ToArray(),
  35. SignName = model.SignName,
  36. SmsSdkAppId = _smsSdkAppId
  37. });
  38. //Console.WriteLine(AbstractModel.ToJsonString(resp));
  39. Console.WriteLine($"[{DateTime.Now:MM-dd HH:mm:ss}]{resp.RequestId}");
  40. foreach (var item in resp.SendStatusSet)
  41. {
  42. Console.WriteLine($"[{DateTime.Now:MM-dd HH:mm:ss}]{item.PhoneNumber},{item.Code},{item.Message}");
  43. }
  44. return resp;
  45. }
  46. catch (Exception ex)
  47. {
  48. Console.WriteLine($"[{DateTime.Now:MM-dd HH:mm:ss}]templateId:{model.TemplateId},mobiles:{string.Join(";", model.Mobiles)},params:{string.Join(";", model.Params)},{ex.Message}");
  49. return null;
  50. }
  51. }
  52. }
  53. }