前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >短信发送接口 – SubMail

短信发送接口 – SubMail

作者头像
前Thoughtworks-杨焱
发布2021-12-08 08:28:18
6.2K0
发布2021-12-08 08:28:18
举报
文章被收录于专栏:杨焱的专栏
代码语言:javascript
复制
public class SubMailSmsSender {

    private static Logger logger = LoggerFactory.getLogger(SubMailSmsSender.class);
    private String submailSendUrl;
    private String submailAppid;
    private String submailAppkey;

    public SubMailSmsSender(String submailSendUrl, String submailAppid, String submailAppkey) {
        this.submailSendUrl = submailSendUrl;
        this.submailAppid = submailAppid;
        this.submailAppkey = submailAppkey;
    }

    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * SUBMAIL 的短信一对多(即1条API请求发送多个号码,并可以灵活控制每个联系人的文本变量)和群发 API 。调用方需要确保 templateId 正确, smsList 中至少有一条
     *
     * @param templateId 短信模板
     * @param multi      批量发送列表json字符串
     * @return 返回发送成功的接收方手机号列表
     */
    public List<String> multixsend(String templateId, String multi) {
        String info = null;
        boolean success = false;
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("appid", submailAppid));
        params.add(new BasicNameValuePair("project", templateId));
        params.add(new BasicNameValuePair("multi", multi));
        params.add(new BasicNameValuePair("signature", submailAppkey));

        try {
            List<String> successPhoneList = new ArrayList<>();
            HttpResponse post = HttpClientHelper.INSTANCE.post(submailSendUrl, params, "utf-8", null, null);
            info = EntityUtils.toString(post.getEntity(), "utf-8");
            if (logger.isDebugEnabled()) {
                logger.debug("SUBMAIL 返回:" + info);
            }
            JsonNode jsonNode = objectMapper.readTree(info);
            Iterator<JsonNode> iterator = jsonNode.iterator();
            while (iterator.hasNext()) {
                JsonNode next = iterator.next();
                String status = next.get("status").asText();
                String to = next.get("to").asText();
                if ("success".equals(status)) {
                    successPhoneList.add(to);
                    if (logger.isInfoEnabled()) {
                        logger.info("成功发送短信给[" + to + "]");
                    }
                } else {
                    String logContent = "发送短信给[" + to + "]失败";
                    if (logger.isInfoEnabled()) {
                        logger.error(logContent);
                    }
                }
            }
            return successPhoneList;
        } catch (IOException e) {
            logger.error("请求 SubMail 服务器发送短信产生IO 错误", e);
            return Collections.emptyList();
        }
    }

    /**
     * SUBMAIL 的短信一对多(即1条API请求发送多个号码,并可以灵活控制每个联系人的文本变量)和群发 API 。调用方需要确保 templateId 正确, smsList 中至少有一条
     *
     * @param templateId 短信模板
     * @param smsList    批量发送列表
     * @return 返回发送成功的接收方手机号列表
     */
    public List<String> multixsend(String templateId, List<SubMailSms> smsList) {

        if (smsList == null || smsList.isEmpty()) {
            return Collections.emptyList();
        }
        String multi = null;
        try {
            multi = objectMapper.writeValueAsString(smsList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return this.multixsend(templateId, multi);
    }

    /**
     * 短信发送信息
     */
    static class SubMailSms {
        private String to;
        private Map<String, String> vars;

        public String getTo() {
            return to;
        }

        public void setTo(String to) {
            this.to = to;
        }

        public Map<String, String> getVars() {
            return vars;
        }

        public void setVars(Map<String, String> vars) {
            this.vars = vars;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-02-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档