标签:on() err nts def 名称 int nbsp client mss
1.首先登陆阿里云平台添加短信模板
https://account.aliyun.com
2.进入国内消息
3.签名种类
4.添加模板
5.代码涉及的参数
product=Dysmsapi
domain=dysmsapi.aliyuncs.com
regionId=cn-hangzhou
defaultConnectTimeout=30000
defaultReadTimeout=30000
*
这两个参数申请签名的时候,阿里云返回的
accessKeyId=
accessKeySecret=
*
msgSignName=签名1
6.
6.代码
import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.json.JSONException; 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; import com.aliyuncs.profile.IClientProfile; import com.github.qcloudsms.SmsSingleSender; import com.github.qcloudsms.SmsSingleSenderResult; import com.github.qcloudsms.httpclient.HTTPException; import com.google.gson.Gson; import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; public class AliSendMsg { /** *@param phone 接收手机号 *@param smscode 短信模版名称 *@param map 短信模版所需参数 */ public static SendSmsResponse sendSms(String phone, String smscode, Map<String, String> map) { SendSmsResponse sendSmsResponse = null; try { Prop prop = PropKit.use("msg.properties"); String defaultConnectTimeout = prop.get("defaultConnectTimeout"); String defaultReadTimeout = prop.get("defaultReadTimeout"); String regionId = prop.get("regionId"); String product = prop.get("product"); String domain = prop.get("domain"); String accessKeyId = prop.get("accessKeyId"); String accessKeySecret = prop.get("accessKeySecret"); // 可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", defaultConnectTimeout); System.setProperty("sun.net.client.defaultReadTimeout", defaultReadTimeout); // 初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); DefaultProfile.addEndpoint(regionId, regionId, product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); // 组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); // 必填:待发送手机号 request.setPhoneNumbers(phone); // 必填:短信签名-可在短信控制台中找到 String signName = prop.get("msgSignName"); request.setSignName(signName); // 必填:短信模板-可在短信控制台中找到 request.setTemplateCode(smscode); // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 Gson gson = new Gson(); // request.setTemplateParam("{\"name\":\"Tom\",\"number\":\"100\",\"code\":\"1234\"}"); if(!map.isEmpty() && map != null){ request.setTemplateParam(gson.toJson(map)); } // 选填-上行短信扩展码(无特殊需求用户请忽略此字段) // request.setSmsUpExtendCode("90997"); // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 // request.setOutId("yourOutId"); // hint 此处可能会抛出异常,注意catch sendSmsResponse = acsClient.getAcsResponse(request); } catch (Exception e) { try { throw new Exception(e); } catch (Exception e1) { e1.printStackTrace(); } } return sendSmsResponse; }
标签:on() err nts def 名称 int nbsp client mss
原文地址:https://www.cnblogs.com/zzlcome/p/11275564.html