从Web应用程序发送短信

时间:2011-02-23 17:14:10

标签: java sms sms-gateway smpp

我正在尝试使用SMPPSim作为SMS网关和smppapi从java应用程序发送短信

这是发送短信的代码

public class SyncTransmitter extends SMPPAPIExample {

private Log logger = LogFactory.getLog(SyncTransmitter.class);

public SyncTransmitter() {
}

public void execute() {
    try {
        logger.info("Binding to the SMSC");

        myConnection = new Connection("localhost", 2775);
        myConnection.autoAckLink(true);
        myConnection.autoAckMessages(true);

        BindResp resp = myConnection.bind(
                Connection.TRANSMITTER,
                "smppclient1",
                "password",
                null);
        /**
        BindResp resp = myConnection.bind(
                Connection.TRANSMITTER,
                "smppclient1",
                "password",
                systemType,
                sourceTON,
                sourceNPI,
                sourceAddress);

         */
        if (resp.getCommandStatus() != 0) {
            logger.info("SMSC bind failed.");
            System.exit(1);
        }

        logger.info("Bind successful...submitting a message.");

        // Submit a simple message
        SubmitSM sm = (SubmitSM) myConnection.newInstance(SMPPPacket.SUBMIT_SM);
        sm.setDestination(new Address(1, 8, "9988776655"));
        sm.setMessageText("This is an example short message.");
        SubmitSMResp smr = (SubmitSMResp) myConnection.sendRequest(sm);

        logger.info("Submitted message ID: " + smr.getMessageId());

        // Unbind.
        UnbindResp ubr = myConnection.unbind();

        if (ubr.getCommandStatus() == 0) {
            logger.info("Successfully unbound from the SMSC");
        } else {
            logger.info("There was an error unbinding.");
        }
    } catch (Exception x) {
        logger.info("An exception occurred.");
        x.printStackTrace(System.err);
    }
    }
}

public static void main(String[] args) {

    System.out.println("Test");
    SyncTransmitter st = new SyncTransmitter();
    st.execute();
    System.out.println("Done….");
}

这是SMPPSim控制台输出

2011.02.23 22:29:03 546 INFO    11 StandardConnectionHandler accepted a connection
2011.02.23 22:29:03 765 INFO    11 Protocol handler is of type StandardProtocolHandler
2011.02.23 22:29:03 765 INFO    11 : BIND_TRANSMITTER:
2011.02.23 22:29:03 765 INFO    11 Hex dump (42) bytes:
2011.02.23 22:29:03 765 INFO    11 0000002A:00000002:00000000:00000001:
2011.02.23 22:29:03 765 INFO    11 736D7070:636C6965:6E743100:70617373:
2011.02.23 22:29:03 765 INFO    11 776F7264:00003400:0000
2011.02.23 22:29:03 781 INFO    11 cmd_len=42,cmd_id=2,cmd_status=0,seq_no=1,sys
tem_id=smppclient1
2011.02.23 22:29:03 781 INFO    11 password=password,system_type=,interface_vers
ion=52,addr_ton=0,addr_npi=0
2011.02.23 22:29:03 781 INFO    11 address_range=
2011.02.23 22:29:03 781 INFO    11
2011.02.23 22:29:03 781 INFO    11 New transmitter session bound to SMPPSim
2011.02.23 22:29:03 781 INFO    11 : BIND_TRANSMITTER_RESP:
2011.02.23 22:29:03 781 INFO    11 Hex dump (24) bytes:
2011.02.23 22:29:03 781 INFO    11 00000018:80000002:00000000:00000001:
2011.02.23 22:29:03 781 INFO    11 534D5050:53696D00:
2011.02.23 22:29:03 781 INFO    11 cmd_len=0,cmd_id=-2147483646,cmd_status=0,seq
_no=1,system_id=SMPPSim
2011.02.23 22:29:03 781 INFO    11
2011.02.23 22:29:03 781 INFO    11 : Standard SUBMIT_SM:
2011.02.23 22:29:03 781 INFO    11 Hex dump (76) bytes:
2011.02.23 22:29:03 781 INFO    11 0000004C:00000004:00000000:00000002:
2011.02.23 22:29:03 781 INFO    11 00000000:01083939:38383737:36363535:
2011.02.23 22:29:03 781 INFO    11 00000000:00000000:00002154:68697320:
2011.02.23 22:29:03 781 INFO    11 69732061:6E206578:616D706C:65207368:
2011.02.23 22:29:03 781 INFO    11 6F727420:6D657373:6167652E:
2011.02.23 22:29:03 781 INFO    11 cmd_len=76,cmd_id=4,cmd_status=0,seq_no=2,ser
vice_type=,source_addr_ton=0
2011.02.23 22:29:03 781 INFO    11 source_addr_npi=0,source_addr=,dest_addr_ton=
1,dest_addr_npi=8
2011.02.23 22:29:03 781 INFO    11 dest_addr=9988776655,esm_class=0,protocol_ID=
0,priority_flag=0
2011.02.23 22:29:03 781 INFO    11 schedule_delivery_time=,validity_period=,regi
stered_delivery_flag=0
2011.02.23 22:29:03 781 INFO    11 replace_if_present_flag=0,data_coding=0,sm_de
fault_msg_id=0,sm_length=33
2011.02.23 22:29:03 781 INFO    11 short_message=This is an example short messag
e.
2011.02.23 22:29:03 781 INFO    11
2011.02.23 22:29:03 796 INFO    11 Validity period is not set: defaulting to 5 m
inutes from now
2011.02.23 22:29:03 796 INFO    11 Generated default validity period=11022322340
3000+
2011.02.23 22:29:03 796 INFO    11 :SUBMIT_SM_RESP:
2011.02.23 22:29:03 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:03 796 INFO    11 Hex dump (18) bytes:
2011.02.23 22:29:03 796 INFO    11 00000012:80000004:00000000:00000002:
2011.02.23 22:29:03 796 INFO    11 3000
2011.02.23 22:29:03 796 INFO    11 cmd_len=0,cmd_id=-2147483644,cmd_status=0,seq
_no=2,message_id=0
2011.02.23 22:29:03 812 INFO    11
2011.02.23 22:29:03 812 INFO    11 SubmitSM processing - response written to con
nection
2011.02.23 22:29:03 812 INFO    23 InboundQueue: no available receiver sessions
- moving message(s) to pending queue

**Exception in thread "Thread-1" java.lang.ClassCastException: [Ljava.lang.Object;
 cannot be cast to [Lcom.seleniumsoftware.SMPPSim.pdu.Pdu;
        at com.seleniumsoftware.SMPPSim.InboundQueue.processQueue(Unknown Source
)
        at com.seleniumsoftware.SMPPSim.InboundQueue.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
2011.02.23 22:29:03 812 INFO    11 : UNBIND:**

2011.02.23 22:29:03 812 INFO    11 Hex dump (16) bytes:
2011.02.23 22:29:03 812 INFO    11 00000010:00000006:00000000:00000003:
2011.02.23 22:29:03 812 INFO    11
2011.02.23 22:29:03 812 INFO    11 cmd_len=16,cmd_id=6,cmd_status=0,seq_no=3
2011.02.23 22:29:03 812 INFO    11
2011.02.23 22:29:03 812 INFO    11 : UNBIND_RESP
2011.02.23 22:29:03 812 INFO    11 Hex dump (16) bytes:
2011.02.23 22:29:03 812 INFO    11 00000010:80000006:00000000:00000003:
2011.02.23 22:29:03 812 INFO    11
2011.02.23 22:29:03 812 INFO    11 cmd_len=0,cmd_id=-2147483642,cmd_status=0,seq
_no=3
2011.02.23 22:29:03 812 INFO    11
2011.02.23 22:29:03 812 INFO    11 StandardConnectionHandler waiting for connect
ion
2011.02.23 22:29:08 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:13 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:18 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:23 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:28 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:33 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:38 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:43 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:48 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:53 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:29:58 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:30:00 250 INFO    24 Processing 0 messages in the delayed inbound
queue
2011.02.23 22:30:03 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:30:08 796 INFO    22 Assessing state of 1 messages in the Outbound
Queue
2011.02.23 22:30:13 796 INFO    22 Lifecycle Service: OutboundQueue is empty  -
waiting

我无法理解问题是什么

另请建议一些开源SMS网关

1 个答案:

答案 0 :(得分:1)

我不确定这个lib,但你可以使用smslib。它需要具有GSM功能的移动设备,它将充当虚拟SMS聚合器。