2014-11-04 689 views
3

我想用JAVA发送短信。谷歌搜索后,我发现SMPP协议将用于它,并偶然发现了下面的源代码。使用SMPP在JAVA中发送短信文本

public class SendSMS 
{ 
public static void main(String[] args) throws Exception 
{ 
    SendSMS obj = new SendSMS(); 
    SendSMS.sendTextMessage("<mobile number>"); 
} 

private TimeFormatter tF = new AbsoluteTimeFormatter(); 

/* 
* This method is used to send SMS to for the given MSISDN 
*/ 
public void sendTextMessage(String MSISDN) 
{ 

    // bind param instance is created with parameters for binding with SMSC 
    BindParameter bP = new BindParameter(
      BindType.BIND_TX, 
      "<user_name>", 
      "<pass_word>", 
      "<SYSTEM_TYPE>", 
      TypeOfNumber.UNKNOWN, 
      NumberingPlanIndicator.UNKNOWN, 
      null); 

    SMPPSession smppSession = null; 

    try 
    { 
     // smpp session is created using the bindparam and the smsc ip address/port 
     smppSession = new SMPPSession("<SMSC_IP_ADDRESS>", 7777, bP); 
    } 
    catch (IOException e1) 
    { 
     e1.printStackTrace(); 
    } 

    // Sample TextMessage 
    String message = "This is a Test Message"; 

    GeneralDataCoding dataCoding = new GeneralDataCoding(false, true, 
      MessageClass.CLASS1, Alphabet.ALPHA_DEFAULT); 

    ESMClass esmClass = new ESMClass(); 

    try 
    { 
     // submitShortMessage(..) method is parametrized with necessary 
     // elements of SMPP submit_sm PDU to send a short message 
     // the message length for short message is 140 
     smppSession.submitShortMessage(
       "CMT", 
       TypeOfNumber.NATIONAL, 
       NumberingPlanIndicator.ISDN, 
       "<MSISDN>", 
       TypeOfNumber.NATIONAL, 
       NumberingPlanIndicator.ISDN, 
       MSISDN, 
       esmClass, 
       (byte) 0, 
       (byte) 0, 
       tF.format(new Date()), 
       null, 
       new RegisteredDelivery(SMSCDeliveryReceipt.DEFAULT), 
       (byte) 0, 
       dataCoding, 
       (byte) 0, 
       message.getBytes()); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

}

但我的源代码遇到的问题是,它需要特定的一组像USER_NAME,pass_word,system_type,SMSC IP地址等参数,我没有任何的线索。我最近才知道SMPP协议,所以我不知道如何让这个代码工作,以完成我的手机发送短信的用例。那么有人可以帮我让这段代码工作,或者引导我到一个我可以学习如何做的地方?

+0

您使用哪个库在网络上创建和发送SMPP包? – gkiko 2014-11-04 07:45:11

+1

首先,您必须向任何SMS服务提供商进行注册,例如cellent等。他们将为您提供SMPP连接的所有细节。 – 2014-11-04 07:45:30

+0

@gkiko我使用jsmpp – 2014-11-04 07:46:57

回答

3

我最近一直在SMPP项目上工作。

我用于SMPP协议的库是OpenSMPP

这里是我班建设和发送SMPP数据

public class SmppTransport implements Transport { 

@Override 
public void send(String url, Map<String, String> map) throws IOException { 
    int smscPort = Integer.parseInt(map.get("port")); 
    String smscHost = map.get("send_url"); 
    String smscUsername = map.get("username"); 
    String smscPassword = map.get("password"); 
    String recipientPhoneNumber = map.get("phone_num"); 
    String messageText = map.get("text"); 

    try { 
     SubmitSM request = new SubmitSM(); 
    // request.setSourceAddr(createAddress(senderPhoneNumber)); // you can skip this 
     request.setDestAddr(createAddress(recipientPhoneNumber)); 
     request.setShortMessage(messageText); 
    // request.setScheduleDeliveryTime(deliveryTime);   // you can skip this 
     request.setReplaceIfPresentFlag((byte) 0); 
     request.setEsmClass((byte) 0); 
     request.setProtocolId((byte) 0); 
     request.setPriorityFlag((byte) 0); 
     request.setRegisteredDelivery((byte) 1); // we want delivery reports 
     request.setDataCoding((byte) 0); 
     request.setSmDefaultMsgId((byte) 0); 

     Session session = getSession(smscHost, smscPort, smscUsername, smscPassword); 
     SubmitSMResp response = session.submit(request); 
    } catch (Throwable e) { 
     // error 
    } 
} 

private Session getSession(String smscHost, int smscPort, String smscUsername, String smscPassword) throws Exception{ 
    if(sessionMap.containsKey(smscUsername)) { 
     return sessionMap.get(smscUsername); 
    } 

    BindRequest request = new BindTransmitter(); 
    request.setSystemId(smscUsername); 
    request.setPassword(smscPassword); 
// request.setSystemType(systemType); 
// request.setAddressRange(addressRange); 
    request.setInterfaceVersion((byte) 0x34); // SMPP protocol version 

    TCPIPConnection connection = new TCPIPConnection(smscHost, smscPort); 
// connection.setReceiveTimeout(BIND_TIMEOUT); 
    Session session = new Session(connection); 
    sessionMap.put(smscUsername, session); 

    BindResponse response = session.bind(request); 
    return session; 
} 

private Address createAddress(String address) throws WrongLengthOfStringException { 
    Address addressInst = new Address(); 
    addressInst.setTon((byte) 5); // national ton 
    addressInst.setNpi((byte) 0); // numeric plan indicator 
    addressInst.setAddress(address, Data.SM_ADDR_LEN); 
    return addressInst; 
} 

} 

的例子,我的运营商给了我这个参数SMPP。有许多配置选项,但这些都是必不可少的

#host = 192.168.10.10 // operator smpp server ip 
#port = 12345   // operator smpp server port 
#smsc-username = "my_user" 
#smsc-password = "my_pass" 
#system-type = "" 
#source-addr-ton = 5 
#source-addr-npi = 0 

所以,如果你想不与GSM服务提供商注册,以测试你的代码,你可以在计算机上模拟SMPP服务器。 SMPPSim是一个伟大的测试项目。下载并在计算机上运行。它可以用多种方式配置,例如请求来自SMPP服务器的传送报告,设置短信失败率和e.t.c.我在linux上测试过SMPPSim。

+0

你可以使用模拟器,而不是运营商, 我们使用这一个它是很好的http://www.seleniumsoftware.com/downloads。html – 2014-11-04 08:08:15

+0

传输接口是哪个包? – kodmanyagha 2015-05-12 10:57:42

+0

我相信'Transport'是我自定义的接口,使用'send'方法。你可以把它与'SmppTransport' – gkiko 2015-05-12 12:42:56

1

使用此模拟器here, 它充当服务提供,在构建并测试您的应用程序之后,您必须更改仅由您提供给您的配置参数(用户名,密码,ip,端口,...)服务提供商。

您可以在conf文件中找到所有连接到此模拟器的配置。

0

SMPP是移动网络运营商/运营商和内容提供商之间的协议。您指定的字段(用户名,密码,SMSC IP)由操作员提供。不幸的是,除非您为内容提供商公司工作,或者与运营商达成交易,否则您不可能获得这些详细信息。

模拟器可以让你测试你的SMPP代码,但它们实际上不会将内容传送到你的手机。

我最好的建议,如果你想从你的Java应用程序发送短信将是使用SMS API,如Twilio's

+0

但是这不回答@aarish如何使用SMPP发送短信的问题。 – collision 2018-02-05 12:47:12