第一次写短信服务,没想到一次就成功了,还是要谢谢同事给我提供的工具类吧,首先需要导入依赖
com.aliyun aliyun-java-sdk-core 3.3.1 com.aliyun aliyun-java-sdk-dysmsapi 1.1.0
然后就是工具类,一个是购买了短信服务的模板填写信息类,
public class GlobalConfigUtils { /** * 微信公众平台相关字段 */ public static final String WxAppId = ""; public static final String WxAppSecret = ""; /** * 阿里云通信发送短信验证码相关 */ //必填:短信签名-可在短信控制台中找到 public static final String AliMsgSignName = ""; //必填:短信模板-可在短信控制台中找到 public static final String AliTemplateCode = ""; // 通知用户发快递的模板 public static final String AliNotichDeliveryTemplateCode = ""; public static final String AliAccessKeyId = ""; public static final String AliAccessKeySecret = "";}
另一个是短信发送的工具类,实现短信发送调用sendMsg(String phoneNumber, String code)就行。
public class AliyunSmsUtils { //产品名称:云通信短信API产品,开发者无需替换 static final String product = "Dysmsapi"; //产品域名,开发者无需替换 static final String domain = "dysmsapi.aliyuncs.com"; //填写自己的appKey static final String accessKeyId = GlobalConfigUtils.AliAccessKeyId; static final String accessKeySecret = GlobalConfigUtils.AliAccessKeySecret; /** * 发送短信验证码 * * @return * @throws ClientException * @throws ServerException */ public static SendSmsResponse sendMsg(String phoneNumber, String code) throws ServerException, ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 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(phoneNumber); //必填:短信签名-可在短信控制台中找到 request.setSignName(GlobalConfigUtils.AliMsgSignName); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(GlobalConfigUtils.AliTemplateCode); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam("{\"number\":" + "\"" + code + "\"}"); //选填-上行短信扩展码(无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者// request.setOutId("yourOutId"); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; } public static SendSmsResponse sendMsg(Admin admin) throws ServerException, ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 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(admin.getPhone()); //必填:短信签名-可在短信控制台中找到 request.setSignName(GlobalConfigUtils.AliMsgSignName); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(GlobalConfigUtils.AliNotichDeliveryTemplateCode); //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.setTemplateParam("{\"name\":" + "\"" + admin.getNickname() + "\"}"); SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; }}