东软SMIAS短信接入Java开发
东软SMIAS短信接入Java开发 package org.crabo.blog;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.commerceware.cmpp.*;
public class SMSAPI {
static Log log;
static cmppe_result result;
static CMPP cmpp;
static conn_desc connDesc;
public static void main(String[] args) {
String loginID = '*****';
String userID = '*****\0';
String password = '*****';
String icpID = '*****\0';
String dst = '*****';
String mess = '*****';
String svcType = '*****\0';
String ip = '*****';
int port = 7890;
////////////////////////////////////////////////////////
log = LogFactory.getLog(SMSAPI.class);
result = null;
cmpp = new CMPP();
connDesc = new conn_desc();
////////////////////////////////////////////////////////
//截取多个联系人
String[] dstArray = dst.split(';');
//计算消息截取段数 :56/70 = 0 => messPart=messPart=1
int messLen = mess.length();
int messPart = messLen/70 +1;
String[] messArray = new String[messPart];
for(int i=0;i<messPart;i++)
{
messArray[i] = mess.substring(70*i,(70*(i+1)<messLen?70*(i+1):messLen))+'\0';
}
////////////////////////////////////////////////////////
log.info('开始运行... ');
try{
cmpp.cmpp_connect_to_ismg(ip, port, connDesc);
log.info('连接完毕!');
cmpp.cmpp_login(connDesc, loginID, password, (byte)2, 0x12, (int)System.currentTimeMillis());
result = cmpp.readResPack(connDesc);
if (result.stat != CMPP.CMPPE_RSP_SUCCESS)
throw new Exception('状态错误,stat '+ result.stat);
else
log.info('登录成功! ');
}catch(Exception e){
log.error('登录失败 ' + e);
System.exit(0);
}
////////////////////////////////////////////////////////
log.info('开始发送... ');
try{
cmppe_submit submitStruct = new cmppe_submit();
submitStruct.set_svctype(svcType.getBytes());
submitStruct.set_srcaddr(userID.getBytes());
submitStruct.set_ducount((byte)1);
submitStruct.set_msgmode((byte)1);
submitStruct.set_icpid(icpID.getBytes());
//submitStruct.set_feetype((byte)1);
//submitStruct.set_infofee(300);
byte[][] dstByte= {{0,0},{0,0}};
byte[] message;
for(int i=0;i<dstArray.length;i++) //逐个联系人
{
dstArray[i] += '\0'; //每个号码必须以byte 0结尾
dstByte[0] = dstArray[i].getBytes();
submitStruct.set_dstaddr(dstByte);
for(int j=0;j<messArray.length;j++) //逐条消息(70个字分组)
{
message = messArray[j].getBytes();
submitStruct.set_msg((byte)15, message.length, message);
cmpp.cmpp_submit(connDesc, submitStruct);
ReadResponse();//Submit
ReadResponse();//Deliver
}
}
}catch(Exception e){
log.error('发送异常 '+ e);
System.exit(1);
}
////////////////////////////////////////////////////////
log.info('登出 ');
try{
cmpp.cmpp_logout(connDesc);
}catch(Exception e)
{
log.error('登出异常 '+ e);
}
}
private static void ReadResponse()
{
try{
result= cmpp.readResPack(connDesc);
switch(result.pack_id){
case CMPP.CMPPE_NACK_RESP:
log.info('Get Nack Pack');
break;
case CMPP.CMPPE_SUBMIT_RESP:
if(result.stat==0)
log.info('Get Sumit Resp. stat = ' + result.stat);
else
log.error('Get Sumit Resp. stat = ' + result.stat);
break;
case CMPP.CMPPE_DELIVER:
cmppe_deliver_result deliverResult = (cmppe_deliver_result)result;
log.info('Get Deliver. stat = ' + deliverResult.stat);
if(deliverResult.stat != 0)
{
log.info('Deliver Status Rpt ' + deliverResult.status_rpt);
log.info('Deliver Src ' + new String(deliverResult.src_addr));
log.info('Deliver Dst ' + new String(deliverResult.dst_addr));
//log.info('Deliver Message '+ new String(deliverResult.short_msg,'GB2312'));
}
cmpp.cmpp_send_deliver_resp(connDesc, deliverResult.seq, deliverResult.stat);
break;
default:
break;
}
}catch(Exception e){
log.error('ReadResponse异常 ' + e);
}
}
}