今天使用到了用移动云短信服务发送短信功能,顺便记录下
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"; //模板idpublic 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>());}}
结束........