package com.yetech; import java.util.Random; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; public class SignDemo { static final String product = "Dysmsapi";//产品名称 static final String domain = "dysmsapi.aliyuncs.com";//产品域名 public static SendSmsResponse sendSms(String phone, String code, String accessKeyId, String accessKeySecret) throws ClientException { //可自助调整超时时间10s System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象 SendSmsRequest request = new SendSmsRequest(); //必填 request.setPhoneNumbers(phone);//待发送手机号 request.setSignName("");//自己定义的短信签名 request.setTemplateCode("");//自己定义的短信模板 //可选 request.setTemplateParam("{"code":""+code+""}"); SendSmsResponse sendSmsResponse = null; try { sendSmsResponse = acsClient.getAcsResponse(request); System.out.println(sendSmsResponse.getMessage() + "------" + sendSmsResponse.getRequestId()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){ System.out.println("短信发送成功!"); }else { System.out.println("短信发送失败!"); } return sendSmsResponse; } public static void main(String[] args) { try { Random random = new Random(); String code = String.valueOf(random.nextInt(9000) + 1000); SignDemo.sendSms("15270170013",code,"你的accessKeyId","你的accessKeySecret"); } catch (ClientException e) { e.printStackTrace(); } } }
需要的阿里jar包
链接:https://pan.baidu.com/s/1wuA3ILwZwp7KD4Bg0n4NWA
提取码:nsuf