发送短信

Posted Ipeter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了发送短信相关的知识,希望对你有一定的参考价值。

//发送短信
    @Override
    public int sendSms(String mobile){

        int smsCode=0, ret=0;
        String temp;
        boolean result=false;
        Map<String, String> paramMap = new HashMap<>();

        //发送短信内容
        smsCode = 1000 + (int)(Math.random()*8999);
        temp = "验证码:"+smsCode+"。请不要把验证码泄露给其他人。如非本人操作,可不用理会";
        paramMap.put("msg", temp);
        paramMap.put("mobile", mobile);

        //获取短信配置
        List<AppSmsSet> appSmsSetList = getAppSmsSetAvail();
        if(appSmsSetList==null){
            return -1;
        }

        //发送短信
        for (int i=0; i<appSmsSetList.size(); i++){
            AppSmsSet appSmsSet = appSmsSetList.get(i);
            paramMap.put("url", appSmsSet.getUrl());
            paramMap.put("password", appSmsSet.getPassword());
            //
            if(appSmsSet.getSmsservicename().equals("九天企信")){
                paramMap.put("cpid", appSmsSet.getCpid());
                paramMap.put("channel", appSmsSet.getChannelid());
                result = SmsUtil.sendSmsJTQX(paramMap);
            }else if (appSmsSet.getSmsservicename().equals("云通讯")){
                paramMap.put("act", appSmsSet.getName());
                result = SmsUtil.sendSmsYTX(paramMap);
            }else {
                //未知接口
                continue;
            }
            //
            if(result){
                break;
            }
        }
        if(!result){
            return -1;
        }

        //保存记录
        AppMobileSmscode appMobileSmscode = getAppMobileSmscode(mobile);
        if(appMobileSmscode==null){
            appMobileSmscode = new AppMobileSmscode();
            appMobileSmscode.setMobile(mobile);
            appMobileSmscode.setSmsCode(smsCode);
            appMobileSmscode.setCreateTime(new Date());
            appMobileSmscode.setUpdateTime(new Date());
            ret = appMobileSmscodeMapper.insert(appMobileSmscode);
        }else {
            appMobileSmscode.setSmsCode(smsCode);
            appMobileSmscode.setUpdateTime(new Date());
            ret = appMobileSmscodeMapper.updateByPrimaryKey(appMobileSmscode);
        }
        if(ret<=0){
            return -1; //保存失败
        }

        return smsCode;
    }

  

以上是关于发送短信的主要内容,如果未能解决你的问题,请参考以下文章

android小知识点代码片段

用于 SMS 的 ContentObserver [重复]

向手机发送短信的python源代码,该怎么解决

阿里鱼注册短信发送代码

谁有TC35i与PC收发短信的代码 包括打开串口 启动模块 接收短信发送短信 PDU解码

阿里大鱼短信发送PHP代码