上海软件培训网站建设,广州地铁封闭最新消息,山西公司网站建设效果,常用于制作网页的软件1、业务需求
发送短信功能是一个很普遍的需求#xff0c;比如验证码#xff0c;快递单号#xff0c;通知信息一类。 而在Java中实现短信功能相对简单#xff0c;只需要调用短信服务商提供的API。接下来以阿里云为例#xff0c;介绍如何实现短信发送功能#xff0c;其他短…1、业务需求
发送短信功能是一个很普遍的需求比如验证码快递单号通知信息一类。 而在Java中实现短信功能相对简单只需要调用短信服务商提供的API。接下来以阿里云为例介绍如何实现短信发送功能其他短信服务商也是类似的操作。 2、短信发送逻辑
阿里云短信发送逻辑大致总结为提供登录阿里云的账号(AccessKey ID)和密码(AccessKey Secret)登录后选择对应的短信签名和模版填充对应的短信内容模版参数然后将短信发送到指定的手机号。 签名短信开头时【】内的公司信息 模板短信内容的骨架可填充对应参数 注意阿里云短信服务申请签名主要针对企业开发个人申请时有一定难度在审核时会审核资质需要上传营业执。但阿里云提供了测试签名和测试模板开发测试时只需要获取对应的测试签名和测试模板。
获取AccessKeyId和AccessKeySecret点击头像右上角的AccessKey管理按钮后获取。 获取测试签名和模板 3、代码实现
目前阿里云短信服务的SDK分为1.0版本和2.0版本1.0已经不再维护推荐使用2.0版本下面列举了两种方式的实现代码。
2.0版本
(1) Maven方式引入pom依赖
dependencygroupIdcom.aliyun/groupIdartifactIddysmsapi20170525/artifactIdversion2.0.24/version
/dependency (2) 代码编写
代码逻辑创建发送短信的客户端提供参数调用对应方法即可。 需要提供下面几个参数
AccessKey ID类似登录阿里云时的账号AccessKey Secret类似登录阿里云的密码phone接收短信的号码signName短信签名即短信开头时【】内的公司信息templateCode模版编码即短信内容的骨架templateParam模版参数即要往短信模版骨架中填充的内容
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teautil.models.RuntimeOptions;public class SMSUtils {/*** 阿里云登录id*/private final static String ACCESS_KEY_ID your access key id;/*** 阿里云登录密码*/private final static String ACCESS_KEY_SECRET your access key secret;/*** 初始化登录阿里云的Client** param accessKeyId* param accessKeySecret* return Client*/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().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);// 可指定登录的服务器地址可参考 https://api.aliyun.com/product/Dysmsapiconfig.endpoint dysmsapi.aliyuncs.com;return new com.aliyun.dysmsapi20170525.Client(config);}/*** 发送短信方法* param phoneNumbers 手机号* param signName 签名* param templateCode 模板编号* param param 模板参数* throws Exception*/public static void sendMessage(String phoneNumbers, String signName, String templateCode, String param) throws Exception {com.aliyun.dysmsapi20170525.Client client SMSUtils.createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest new com.aliyun.dysmsapi20170525.models.SendSmsRequest().setPhoneNumbers(phoneNumbers).setSignName(signName).setTemplateCode(templateCode).setTemplateParam(param);SendSmsResponse sendSmsResponse client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());System.out.println(短信发送成功返回结果是sendSmsResponse);}public static void main(String[] args) {try {SMSUtils.sendMessage(15179068888, 阿里云短信测试, SMS_154958888, {\code\:\7777\});} catch (Exception e) {throw new RuntimeException(e);}}} 1.0版本
(1) Maven方式引入pom依赖
dependencygroupIdcom.aliyun/groupIdartifactIdaliyun-java-sdk-core/artifactIdversion4.5.16/version
/dependency
dependencygroupIdcom.aliyun/groupIdartifactIdaliyun-java-sdk-dysmsapi/artifactIdversion2.1.0/version
/dependency (2) 代码编写
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.profile.DefaultProfile;/*** 短信发送工具类*/
public class SMSUtils {/*** 发送短信* param signName 签名* param templateCode 模板* param phoneNumbers 手机号* param param 模板参数*/public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){// client的参数服务器ip accessKeyId accessKeySecret DefaultProfile profile DefaultProfile.getProfile(cn-hangzhou, xxxxxxxxxxxxxxxx, xxxxxxxxxxxxxx);// 登录clientIAcsClient client new DefaultAcsClient(profile);SendSmsRequest request new SendSmsRequest();request.setSysRegionId(cn-hangzhou);request.setPhoneNumbers(phoneNumbers);request.setSignName(signName);request.setTemplateCode(templateCode);request.setTemplateParam({\code\:\param\});try {SendSmsResponse response client.getAcsResponse(request);System.out.println(短信发送成功);}catch (ClientException e) {e.printStackTrace();}}
}