就在昨天收到了阿里云要在2018年1月22日之前迁移短信服务,为此,在这里记下新的短信发送版本java
1 package com.bestway.program.common.task; 2 3 import com.aliyuncs.DefaultAcsClient; 4 import com.aliyuncs.IAcsClient; 5 import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; 6 import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; 7 import com.aliyuncs.exceptions.ClientException; 8 import com.aliyuncs.profile.DefaultProfile; 9 import com.aliyuncs.profile.IClientProfile; 10 11 /** 12 * 短信发送 13 */ 14 public class SmsSend { 15 16 //产品名称:云通信短信API产品,开发者无需替换 17 static final String product = "Dysmsapi"; 18 //产品域名,开发者无需替换 19 static final String domain = "dysmsapi.aliyuncs.com"; 20 static final String accessKeyId = "LTAIjPRMl8bQdj7d"; 21 static final String accessKeySecret = "Ld7WoG4d0ICWjfFdpNrLS8q7E3VqXd"; 22 static final String signName = "王尧";//签名 23 static final String template = "SMS_119085249";//模板code 24 25 public static void sendSms(String port) throws ClientException { 26 //可自助调整超时时间 27 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); 28 System.setProperty("sun.net.client.defaultReadTimeout", "10000"); 29 30 //初始化acsClient,暂不支持region化 31 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); 32 DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); 33 IAcsClient acsClient = new DefaultAcsClient(profile); 34 35 //组装请求对象-具体描述见控制台-文档部分内容 36 SendSmsRequest request = new SendSmsRequest(); 37 //必填:待发送手机号 38 request.setPhoneNumbers("13260648440"); 39 //必填:短信签名-可在短信控制台中找到 40 request.setSignName(signName); 41 //必填:短信模板-可在短信控制台中找到 42 request.setTemplateCode(template); 43 //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 44 request.setTemplateParam("{\"port\":\"" + port + "\"}"); 45 } 46 47 public static void main(String[] args) throws ClientException, InterruptedException { 48 //发短信 49 sendSms("12323123"); 50 } 51 }
但是这里要导入两个包:
如果是直接导入jar,可以跳过,这里通过maven导入的方式:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>RELEASE</version>
</dependency>
另外一个包需要先导入导入本地,在引用:
-Dfile 为自己的jar包的目录,
mvn install:install-file -Dfile=aliyun-java-sdk-dysmsapi-1.0.0.jar -DgroupId=com.aliyun -DartifactId=aliyun-java-sdk-dysmsapi-1.0.0 -Dversion=1.0.0 -Dpackaging=jar