使用移动云短信服务发送普通短信与模板短信

今天使用到了用移动云短信服务发送短信功能,顺便记录下

apid,secretKey和集团名称分别为如下图用户名,密码,所属分组

package com.keyou.proj.authentication.service.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import java.io.IOException;
import java.util.*;


/**
 * 移动云短信发送
 * @author huwenjie
 * @Date 2024-03-07
 */
@Slf4j
public class MasSmsUtil {

    private static String apId="*******"; //用户名
    private static String secretKey="********"; //密码
    private static String ecName = "**************"; //集团名称
    private static String sign = "**********"; //网关签名编码
    private static String addSerial = ""; //拓展码 填空
    private static String templateId = "cdd39593dd954bca9a2a13907e4e15a3"; //模板id
    public static String url = "http://112.35.1.155:1992/sms/norsubmit"; //http请求 发送普通短信
    public static String tmpUrl = "http://112.35.1.155:1992/sms/tmpsubmit"; //http请求 发送模板短信

    /**
     * 多用户发送普通短信信息
     * @param mobiles 手机号码逗号分隔
     * @param content 短信内容
     * @throws Exception
     */
    public static void sendMsg(String mobiles, String content) throws Exception {
        HashMap<String, Object> params = new HashMap<>();
        params.put("ecName",ecName);
        params.put("apId",apId);
        params.put("secretKey",secretKey);
        params.put("mobiles",mobiles);
        params.put("content",content);
        params.put("sign",sign);
        params.put("addSerial",addSerial);
        String str = ecName + apId + secretKey  + mobiles + content + sign + addSerial;
        String mdSe = SecureUtil.md5(str).toLowerCase();
        params.put("mac",mdSe);
        String reqText = JSON.toJSONString(params);
        String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8"));
        String body = HttpRequest.post(url).body(encode).execute().body();
        Map map = JSONUtil.toBean(body, Map.class);
        Boolean success = (Boolean)map.get("success");
        if (!success){
            log.error("模板短信发送失败,错误码: " + map.get("rspcod"));
        }
    }



    /**
     * 多用户发送模板短信信息
     * @param mobiles 手机号码逗号分隔
     * @param par 参数
     * @throws Exception
     */
    public static void sendTmpMsg(String mobiles, List<String> par) throws Exception {
        HashMap<String, Object> params = new HashMap<>();
        params.put("ecName",ecName);
        params.put("apId",apId);
        params.put("secretKey",secretKey);
        params.put("templateId",templateId);
        params.put("mobiles",mobiles);
        params.put("params",par);
        params.put("sign",sign);
        params.put("addSerial",addSerial);

        String str = ecName + apId + secretKey + templateId + mobiles + JSONUtil.toJsonStr(par) + sign + addSerial;
        String mdSe = SecureUtil.md5(str).toLowerCase();
        params.put("mac",mdSe);
        String reqText = JSON.toJSONString(params);
        String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8"));

        String body = HttpRequest.post(tmpUrl).body(encode).execute().body();
        Map map = JSONUtil.toBean(body, Map.class);
        Boolean success = (Boolean)map.get("success");
        if (!success){
            log.error("模板短信发送失败,错误码: " + map.get("rspcod"));
        }
    }

    public static void main(String[] args) throws Exception {
//        sendMsg("182****076","这是一个测试短信哦");
        sendTmpMsg("182******076",new ArrayList<String>());
    }

}

结束........

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值