简体   繁体   English

使用 SMPP 在 JAVA 中发送短信

[英]Using SMPP to send sms texts in JAVA

I am trying to send sms using JAVA.我正在尝试使用 JAVA 发送短信。 After googling, I found out that SMPP protocol is to be used for it and stumbled upon the below source code.谷歌搜索后,我发现要使用 SMPP 协议并偶然发现以下源代码。

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();
    }
}

} }

But the problem I encounter with the source code is that it requires specific set of parameters like user_name, pass_word, system_type, SMSC IP address etc which I have no clue of.但是我在源代码中遇到的问题是它需要一组特定的参数,例如 user_name、pass_word、system_type、SMSC IP 地址等,我对此一无所知。 I have only recently known about the SMPP protocol and so am unaware of how to get this code working to fulfil my usecase of sending sms to my mobile.我最近才知道 SMPP 协议,所以我不知道如何让这个代码工作来实现我将短信发送到我的手机的用例。 So can someone please help me get this code to work or guide me to a place where i can learn about doing this?那么有人可以帮我让这个代码工作或引导我到一个我可以学习这样做的地方吗?

I've been working on SMPP project recently.我最近一直在研究 SMPP 项目。

The library I used for SMPP protocol is OpenSMPP .我用于 SMPP 协议的库是OpenSMPP

Here is the example of my class for building and sending SMPP data这是我用于构建和发送 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;
}

}

And my operator gave me this parameters for SMPP.我的接线员给了我这个 SMPP 参数。 There are many configuration options but these are essential有很多配置选项,但这些是必不可少的

#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

So if you want to test your code without registering with GSM service provider, you can simulate SMPP server on your computer.因此,如果您想在不向 GSM 服务提供商注册的情况下测试您的代码,您可以在您的计算机上模拟 SMPP 服务器。 SMPPSim is a great project for testing. SMPPSim是一个很棒的测试项目。 Download it and run on your computer.下载它并在您的计算机上运行。 It can be configured in multiple ways eg request delivery reports from SMPP server, set sms fail ratio and etc I've tested SMPPSim on linux.它可以通过多种方式进行配置,例如从 SMPP 服务器请求交付报告、设置短信失败率等我已经在 linux 上测试过 SMPPSim。

Use following code for single class execution:使用以下代码执行单个类:

public class SmppTransport {

static Map sessionMap=new HashMap<String,String>(); 
String result=null;
public String send(String url, Map<String, String> map) throws Exception {
    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);
        result=new String(response.toString());
    } catch (Exception e) {
        result=StackTraceToString(e);
    }
    return result;
}

private Session getSession(String smscHost, int smscPort, String smscUsername, String smscPassword) throws Exception{

    if(sessionMap.containsKey(smscUsername)) {
        return (Session) sessionMap.get(smscUsername);
    }

    BindRequest request = new BindTransmitter();
    request.setSystemId(smscUsername);
    request.setPassword(smscPassword);
    request.setSystemType("smpp");

 // 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.toString());

    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;
}

public String StackTraceToString(Exception err) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    err.printStackTrace(pw);
    return sw.toString();
    }



public String sendSMS(String Port, String Host,String SMPPUserName,String SMPPPassword,String Phone_Number,String Message) throws Exception {
    String response=null;
    sessionMap.put("port",Port);
    sessionMap.put("send_url",Host);    
    sessionMap.put("username",SMPPUserName);
    sessionMap.put("password",SMPPPassword);
    sessionMap.put("phone_num",Phone_Number);
    sessionMap.put("text",Message);
    Set set=sessionMap.entrySet();//Converting to Set so that we can traverse  
    Iterator itr=set.iterator(); 
    while(itr.hasNext()){  

      Map.Entry entry=(Map.Entry)itr.next();  
           }  

    SmppTransport test =new SmppTransport();
    try {
        response=test.send("10.50.**.**", sessionMap);
        System.out.println(response);
    } catch (Exception e) {

        response=StackTraceToString(e);
    }
    return response;

}

public static void main(String[] args) throws Exception
{
    SmppTransport sm=new SmppTransport();
    String test=sm.sendSMS("80*6", "10.50.**.**", "f***obi", "x***fQ", "+9187965*****", "Testing1");
    System.out.println("Data: "+test);

}}

Use this simulator here , It acts as a service provide, after build and test your application on it you have to change just config parameters(username, password, ip, port, ...) that provided to you by the service provider .在此处使用此模拟器,它充当服务提供者,在其上构建和测试您的应用程序后,您只需更改服务提供者提供给您的配置参数(用户名、密码、ip、端口等)。

you can find all configurations to connect to this simulator in conf file.您可以在 conf 文件中找到连接到此模拟器的所有配置。

SMPP is a protocol between mobile network operators/carriers and content providers. SMPP 是移动网络运营商/运营商和内容提供商之间的协议。 The fields you specified (username, password, SMSC IP) are provisioned from the operators.您指定的字段(用户名、密码、SMSC IP)由运营商提供。 Unfortunately, unless you work for a content provider company, or have a deal with an operator, you are unlikely to get these details.不幸的是,除非您为内容提供商公司工作,或者与运营商有交易,否则您不太可能获得这些详细信息。

Simulators can let you test out your SMPP code but they will not actually deliver content to your phone.模拟器可以让您测试您的 SMPP 代码,但它们实际上不会将内容传送到您的手机。

My best advice if you want to send SMS from your Java app would be to use an SMS API like Twilio's .如果您想从 Java 应用程序发送 SMS,我的最佳建议是使用像Twilio 的.

SMPP account details such as username (system ID), password, SMSC IP or hostname, and port are provided by operators or by SMS aggregators. SMPP帐户详细信息(例如用户名(系统ID),密码,SMSC IP或主机名以及端口)由运营商或SMS聚合商提供。 Many aggregators provide SMPP account details and you can easily sign-up for such services online. 许多聚合器提供SMPP帐户详细信息,您可以轻松地在线注册此类服务。 A couple are listed below: 下面列出了几个:

https://smscarrier.com https://smscarrier.com

https://world-text.com https://world-text.com

Online SMSC simulators also provide SMPP account details to enable you to test your SMS application. 在线SMSC模拟器还提供SMPP帐户详细信息,使您可以测试SMS应用程序。 One such simulator is: 一种这样的模拟器是:

https://melroselabs.com/services/smsc-simulator/ https://melroselabs.com/services/smsc-simulator/

To find out more about the SMPP protocol, see https://smpp.org . 要了解有关SMPP协议的更多信息,请参见https://smpp.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM