java集成阿里云短信服务

一、 导入两个jar


二、

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AliyunTXSms {

    private static Logger logger = LoggerFactory.getLogger("sysLog");

    //阿里云短信服务regionID
    private static final String REGIONID = "cn-hangzhou";
    //阿里云短信服务key
    private static final String ACCESSKEY = "";
    //阿里云短信服务secret
    private static final String ACCESSSECRET = "";
    //模板码
    private static final String CODE = "";
    //签名
    private static final String SIGNNAME = "高";

    public static void sendSms(String phone, String jsonStr) {

        // 初始化ascClient需要的几个参数
        final String product = "Dysmsapi";// 短信API产品名称
        final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名

        try {
            IClientProfile profile = DefaultProfile.getProfile(REGIONID, ACCESSKEY, ACCESSSECRET);

            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou",
                    product, domain);

            IAcsClient client = new DefaultAcsClient(profile);

            //组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            //使用post提交
//            request.setMethod(MethodType.POST);
            // 必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
            request.setPhoneNumbers(phone);
            // 必填:短信签名-可在短信控制台中找到
            request.setSignName(SIGNNAME);
            // 必填:短信模板-可在短信控制台中找到
            request.setTemplateCode(CODE);
            //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
            //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
            request.setTemplateParam(jsonStr);

            //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
//            request.setOutId("yourOutId");

            SendSmsResponse sendSmsResponse = client.getAcsResponse(request);
            if (sendSmsResponse.getCode() != null
                    && sendSmsResponse.getCode().equals("OK")) {
                // 请求成功
                logger.info(phone + "短信发送成功");
            } else {
                logger.info(phone + "短信发送失败");
                logger.error("短信发送失败code=" + sendSmsResponse.getCode());
                logger.error("短信发送失败message=" + sendSmsResponse.getMessage());
                logger.error("此号码频繁发送验证码,暂时不能获取!");
            }
            String requestId = sendSmsResponse.getRequestId();
            logger.info("requestId:" + requestId);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
private string findpwdmobilecode(string telphone) { bool result = false; string qiyecid = "k游";//签名,例如:【阿里大于】 string appkey = "L1111111111111111111V"; string secret = "E111111111111111111111111"; string sms = "SMS_111111111";//模板ID,在阿里大于管理后台添加审核 string url = "http://gw.api.taobao.com/router/rest";//阿里大于API接口地址 string fscode = "";//随机验证码 Random random = new Random(); for (int i = 0; i < 4; i++) { fscode += random.Next(9); } Session["mobilecode"] = fscode + telphone; this.Session.Timeout = 5; #region 新版阿里大鱼 IClientProfile profile = DefaultProfile.GetProfile("default", appkey, secret); DefaultAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.Method = MethodType.POST; request.Domain = "dysmsapi.aliyuncs.com"; request.Version = "2017-05-25"; request.Action = "SendSms"; // request.Protocol = ProtocolType.HTTP; request.AddQueryParameters("PhoneNumbers", telphone); request.AddQueryParameters("SignName", qiyecid); request.AddQueryParameters("TemplateCode", sms); string sendcode = "{\"code\":\"" + fscode + "\"}"; request.AddQueryParameters("TemplateParam", sendcode); string err_code = ""; try { CommonResponse response = client.GetCommonResponse(request); string jsonText = System.Text.Encoding.Default.GetString(response.HttpResponse.Content); try { JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); err_code = jo["Code"].ToString(); } catch (Exception e) { return ""; } switch (err_code)//返回1代表成功 { case "OK": result = true;//验证码已经发送
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值