SendSmsSync.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.miaxis.common.sms;
  2. // This file is auto-generated, don't edit it. Thanks.
  3. import com.aliyun.tea.*;
  4. import org.springframework.beans.factory.annotation.Value;
  5. public class SendSmsSync {
  6. @Value("${aliyun.accessKeyId}")
  7. private static String accessKeyId;
  8. @Value("${aliyun.accessKeySecret}")
  9. private static String accessKeySecret;
  10. /**
  11. * 使用AK&SK初始化账号Client
  12. * @param accessKeyId
  13. * @param accessKeySecret
  14. * @return Client
  15. * @throws Exception
  16. */
  17. public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
  18. com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
  19. // 必填,您的 AccessKey ID
  20. .setAccessKeyId(accessKeyId)
  21. // 必填,您的 AccessKey Secret
  22. .setAccessKeySecret(accessKeySecret);
  23. // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
  24. config.endpoint = "dysmsapi.aliyuncs.com";
  25. return new com.aliyun.dysmsapi20170525.Client(config);
  26. }
  27. public static void main(String[] args_) throws Exception {
  28. java.util.List<String> args = java.util.Arrays.asList(args_);
  29. // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  30. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
  31. com.aliyun.dysmsapi20170525.Client client = SendSmsSync.createClient("", "");
  32. com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
  33. .setPhoneNumbers("15060063160")
  34. .setSignName("极速驾培")
  35. .setTemplateCode("SMS_463607185")
  36. .setTemplateParam("{\"name\":\"周淑萍\",\"idcard\":\"350526198708052015\",\"password\":\"123456\"}");
  37. com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
  38. try {
  39. // 复制代码运行请自行打印 API 的返回值
  40. client.sendSmsWithOptions(sendSmsRequest, runtime);
  41. } catch (TeaException error) {
  42. // 如有需要,请打印 error
  43. com.aliyun.teautil.Common.assertAsString(error.message);
  44. error.printStackTrace();
  45. } catch (Exception _error) {
  46. TeaException error = new TeaException(_error.getMessage(), _error);
  47. // 如有需要,请打印 error
  48. com.aliyun.teautil.Common.assertAsString(error.message);
  49. }
  50. }
  51. public static boolean sendSms() throws Exception {
  52. // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  53. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
  54. com.aliyun.dysmsapi20170525.Client client = SendSmsSync.createClient(accessKeyId, accessKeySecret);
  55. com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
  56. .setPhoneNumbers("15060063160")
  57. .setSignName("极速驾培")
  58. .setTemplateCode("SMS_463607185")
  59. .setTemplateParam("{\"name\":\"周淑萍\",\"idcard\":\"350526198708052015\",\"password\":\"123456\"}");
  60. com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
  61. try {
  62. // 复制代码运行请自行打印 API 的返回值
  63. client.sendSmsWithOptions(sendSmsRequest, runtime);
  64. } catch (TeaException error) {
  65. // 如有需要,请打印 error
  66. com.aliyun.teautil.Common.assertAsString(error.message);
  67. error.printStackTrace();
  68. } catch (Exception _error) {
  69. TeaException error = new TeaException(_error.getMessage(), _error);
  70. // 如有需要,请打印 error
  71. com.aliyun.teautil.Common.assertAsString(error.message);
  72. }
  73. return false;
  74. }
  75. }