Code to send SMS using J2ME

Josh picture Josh · May 15, 2009 · Viewed 8.9k times · Source

I am looking for code to send SMS using J2ME.

Answer

Nilesh picture Nilesh · Apr 18, 2012

You can try the code below to implement this:

private boolean SendSMS(String sPhoneNo, String sMessage) {
    boolean result = true;

    try {
        String addr = "sms://" + sPhoneNo;
        MessageConnection conn = (MessageConnection) Connector.open(addr);
        TextMessage msg = (TextMessage) 
            conn.newMessage(MessageConnection.TEXT_MESSAGE);
        msg.setPayloadText(sMessage);
        conn.send(msg);
        conn.close();
    } 

    catch (SecurityException se) {
        result = false;
    } 

    catch (Exception e) {
        result = false;
    }

    return result;
}  

You can specify any special port by just adding ":port_no" after:

"String addr = "sms://" + sPhoneNo"