| | |
| | | import cn.hutool.core.util.XmlUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import cn.hutool.http.HttpUtil; |
| | | import cn.hutool.json.JSONObject; |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.aliyun.dysmsapi20170525.models.SendSmsResponse; |
| | | import com.aliyun.tea.TeaException; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.util.Date; |
| | |
| | | } |
| | | } |
| | | |
| | | public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception { |
| | | com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() |
| | | // 您的 AccessKey ID |
| | | .setAccessKeyId(accessKeyId) |
| | | // 您的 AccessKey Secret |
| | | .setAccessKeySecret(accessKeySecret); |
| | | // 访问的域名 |
| | | config.endpoint = "dysmsapi.aliyuncs.com"; |
| | | return new com.aliyun.dysmsapi20170525.Client(config); |
| | | } |
| | | |
| | | private static final String SIGNNAME = "国致优品"; |
| | | private static final String TEMPLATECODE = "SMS_251041117"; |
| | | public static boolean sendALiYun(String phone,String TemplateParam){ |
| | | com.aliyun.dysmsapi20170525.Client client = null; |
| | | try { |
| | | client = createClient("LTAI5tAr6BuXERQbu5NFuFFh", "arpJg4KOTcp9O2jCMGczuGnGdIqb7h"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest() |
| | | // .setSignName("阿里云短信测试") |
| | | .setSignName(SIGNNAME) |
| | | // .setTemplateCode("SMS_154950909") |
| | | .setTemplateCode(TEMPLATECODE) |
| | | // .setPhoneNumbers("15274802129") |
| | | .setPhoneNumbers(phone) |
| | | // .setTemplateParam("{\"code\":\"1234\"}"); |
| | | .setTemplateParam("{\"code\":\""+TemplateParam+"\"}"); |
| | | com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); |
| | | try { |
| | | // 复制代码运行请自行打印 API 的返回值 |
| | | SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime); |
| | | log.info("短信发送:{}", sendSmsResponse); |
| | | JSONObject jsonObject = JSONUtil.parseObj(sendSmsResponse); |
| | | System.out.print(jsonObject); |
| | | if(200 == sendSmsResponse.getStatusCode()) { |
| | | return true; |
| | | }else{ |
| | | return false; |
| | | } |
| | | } catch (TeaException error) { |
| | | // 如有需要,请打印 error |
| | | com.aliyun.teautil.Common.assertAsString(error.message); |
| | | } catch (Exception _error) { |
| | | TeaException error = new TeaException(_error.getMessage(), _error); |
| | | // 如有需要,请打印 error |
| | | com.aliyun.teautil.Common.assertAsString(error.message); |
| | | } |
| | | return false; |
| | | |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | Map<String, Object> data = new HashMap<>(); |
| | | Long time = System.currentTimeMillis(); |