一、开通短信服务
二、添加短信签名、模板
- 这里以国内短信为例
1.点击添加签名
2.填入签名信息后提交
短信签名内容会拼接在短信的开头,通常为公司名称等信息
3.提交后添加模板
短信模板即为发给用户的短信内容,固定不变
4.填入模板信息
注意,控制台这里要求填入模板信息时必须填入签名,实际并没有绑定关系,后续发送短信,可以随意选择审核通过的签名+模板
5.等待签名和模板审核通过
审核时间通常为2小时左右,也可以提交工单,提供审核工单号联系人工审核
三、发送短信
1.控制台发送
2.API接口发送
(1)、添加依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.2.1</version>
</dependency>
(2)、java code
package messageService;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.dysmsapi.model.v20170525.*;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.0</version>
</dependency>
*/
public class SendSms {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<ak>", "<sk>");
/** use STS Token
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // The region ID
"<your-access-key-id>", // The AccessKey ID of the RAM account
"<your-access-key-secret>", // The AccessKey Secret of the RAM account
"<your-sts-token>"); // STS Token
**/
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers("用户的号码");
request.setSignName("签名");
request.setTemplateCode("模板code");
request.setTemplateParam("{\"name\":\"路由器\"}");
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
#####(3)、运行结果
确认收到短信