java 发送短信

项目中需要发送短信.使用的是中国移动的EMPP开发 发送短信.测试代码如下


EmppApi emp=new EmppApi();
//短信监听
SmsListener smsListener=new SmsListener();
try {
String host="211.136.163.68";
int port=9981;
//这个需要你自己去申请帐号
String accountId="";
String passwd="";

EMPPConnectResp response=emp.connect(host, port, accountId, passwd,smsListener);
if (response == null) {
System.out.println("连接超时失败");
return;
}
if (!emp.isConnected()) {
System.out.println("连接失败:响应包状态位=" + response.getStatus());
return;
}
String content="测试";

if(emp.isSubmitable()){
emp.submitMsg(content, new String[]{"手机号码"}, accountId);
}

} catch (MessageIncompleteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownCommandIdException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidEMPPObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ValueNotSetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotEnoughDataInByteBufferException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HeaderIncompleteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (EMPPObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



SmsListener implements EMPPRecvListener 有三个方法
   public void onMessage(EMPPObject message){
     if(message instanceof EMPPUnAuthorization){
                       System.out.println("客户端无权执行此操作");
            return;
         }
        if(message instanceof EMPPSubmitSMResp){
        EMPPSubmitSMResp resp=(EMPPSubmitSMResp)message;
        System.out.println("收到sumbitResp:");
                System.out.println("result="+resp.getResult());
        return;
        }
} //
//处理连接断掉事件
     public void OnClosed(Object object){
      if(object instanceof EMPPTerminate){
            System.out.println("收到服务器发送的Terminate消息,连接终止");
            return;
        }
        SmsListener listener = new SmsListener(emppApi)
        emppApi.reConnect(listener);//重新链接
  }
/异常处理
public void OnError(Exception e)

猜你喜欢

转载自zeng7960983.iteye.com/blog/1827876