AliySms.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Text;
  2. using Aliyun.Acs.Core;
  3. using Aliyun.Acs.Core.Exceptions;
  4. using Aliyun.Acs.Core.Http;
  5. using Aliyun.Acs.Core.Profile;
  6. using Datory.Utils;
  7. namespace GxPress.Common.Sms
  8. {
  9. public static class AliySms
  10. {
  11. public static bool SendSms(string phone, string code)
  12. {
  13. //发送验证码阿里云
  14. IClientProfile profile =
  15. DefaultProfile.GetProfile("cn-hangzhou", "LTAI2E47R4DlcYfo", "5epQRUGRrDSoF7yukyYf4HX6dUlvF3");
  16. DefaultAcsClient client = new DefaultAcsClient(profile);
  17. CommonRequest request = new CommonRequest
  18. {
  19. Method = MethodType.POST,
  20. Domain = "dysmsapi.aliyuncs.com",
  21. Version = "2017-05-25",
  22. Action = "SendSms"
  23. };
  24. request.AddQueryParameters("PhoneNumbers", $"{phone}");
  25. request.AddQueryParameters("SignName", "泰德合众");
  26. request.AddQueryParameters("TemplateCode", "SMS_193518575");
  27. request.AddQueryParameters("TemplateParam", "{\"code\":\"" + code + "\"}");
  28. try
  29. {
  30. CommonResponse response = client.GetCommonResponse(request);
  31. if (response.HttpStatus == 200)
  32. {
  33. var codeByte = Encoding.UTF8.GetBytes(Utilities.JsonSerialize(code));
  34. return true;
  35. }
  36. }
  37. catch (ServerException e)
  38. {
  39. return false;
  40. }
  41. return false;
  42. }
  43. }
  44. }