电信SMGP协议,基于开源的jar文件smgpapi20100113.jar进行实现

        近来做了一个电信项目,电信要求使用它们的短信接口SMGP进行短信通知.找了很多方法, 刚开始乱码,各种处理,后来通过smgpapi20100113.jar,以及其源码已经完了功能的实现。特此记录;

smgpapi20100113.jar及其源码下载地址: 

或我本站分享:http://download.csdn.net/detail/pd10340927/9402069

        电信分为长短信和短短信,具体实现如下:

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

import org.springframework.core.io.ClassPathResource;

 

import Sample.Config;

import cn.com.zjtelecom.smgp.Client;

import cn.com.zjtelecom.smgp.bean.Result;

import cn.com.zjtelecom.smgp.bean.Submit;

public classSmgpUtil {

   private static final String CONFURL = "config/smgp.properties";

   private static final String smgpHost = getConfigValue("smgpHost");//短信接口服务器ip

   private static final String smgpPort = getConfigValue("smgpPort");//服务器端口

   private static final String smgpUser = getConfigValue("smgpUser");//用户名

   private static final String smgpPwd = getConfigValue("smgpPwd");//密码

   private static final String smgpNo = getConfigValue("smgpNo");//电信提供的发送号码

   /**

    * 发送长短信

    * @param phoneNo  接收端号码

    * @param msg      消息内容

    * @throws IOException

    */

   public static void doSendLongMsg(String phoneNo,String msg) throws IOException{

      //soid一般指你的消息来源,如测试,或者来自XX项目的什么功能,参数传2和传0 是制定电信本次socket连接的具体用法,如发短信,收短信,还是其他

        Client client = new Client(smgpHost, Integer.valueOf(smgpPort.trim()), 2, smgpUser, smgpPwd, "spid", 0);

        Submit submit = new Submit();

        submit.setSrcTermid(smgpNo);

        submit.setDestTermid(phoneNo);

        //短消息内容体的编码格式。

        //0ASCII 编码;

        //3=短消息写卡操作;

        //4=二进制短消息;

        //8UCS2 编码;

        //15GB18030 编码;

        //246F6)=UIM 相关消息 UIM 相关消息,用于与 UIM 卡相关的 OTA 等业务,终端

        //收到该类型消息直接转发给 UIM 卡,由 UIM 卡来处理该类型消息;

        //其它保留。

        //对于文字短消息,要求 MsgFormat15。对于回执消息,要求 MsgFormat0 

        submit.setMsgFormat(15);

        submit.setMsgContent(msg.getBytes("GB18030")); //将字符集编码和你设置的msgFormat对应

        submit.setProductID("产品ID");//可以为空

        submit.setMsgType(0);

        submit.setNeedReport(0);

        submit.setPriority(0);

        //serviceId表示业务代码,是该条短消息所属的业务类别,由数字、字母

        //和符号组合而成。对于从 WEB 上发送的点对点短消息,要求业务代码为 “PC2P” ,其它业务代码由 SP 自定义。

        submit.setServiceID("PC2P");

        // 发送短信

        Result[] result = client.SendLong(submit);

        for (int i = 0; i < result.length; i++) {

                System.out.println("--------------------------------");

                System.out.println("Message "+i+"");

                System.out.println("Status:"+ result[i].ErrorCode);

                System.out.println("MsgID:"+ result[i].ErrorDescription);

                System.out.println("--------------------------------");

        }

        // 退出

        client.Close();

   }

   /**

    * 发送短短信

    * @param phoneNo  接收端号码

    * @param msg      消息内容

    * @throws IOException

    */

   public static void doSendShortMsg(String phoneNo,String msg) throws IOException{

      //初始化client

      Clientclient= newClient(smgpHost,Integer.valueOf(smgpPort.trim()), 2, smgpUser, smgpPwd,"spid",0);

      //设置submit

      Submitsubmit=newSubmit();

      submit.setSrcTermid(smgpNo);

      submit.setDestTermid(phoneNo);

      submit.setProductID("产品ID");//可以为空

        submit.setMsgType(0);

        submit.setNeedReport(0);

        submit.setPriority(0);

        submit.setServiceID("PC2P");

        submit.setMsgFormat(15);

      submit.setMsgContent(msg.getBytes("GB18030"));

      Result  result =client.Send(submit);

      System.out.println("Status:"+result.ErrorCode);

      System.out.println("MsgID:"+result.ErrorDescription);

      //退出

      client.Close();

   }

   privatestaticString getConfigValue(String key) {

 

   Propertiesconfig= newProperties();

   InputStreamin = null;

   try {

       config.load(newClassPathResource(CONFURL).getInputStream());

   }catch(FileNotFoundException e) {

       e.printStackTrace();

   }catch(IOException e) {

       e.printStackTrace();

   }finally{

       if (in != null){

      try {

          in.close();

      }catch(IOException e) {

          e.printStackTrace();

      }

       }

   }

   return config.getProperty(key);

   }

}

 


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值