4

I am looking for code to send SMS using J2ME.

halfer
  • 19,824
  • 17
  • 99
  • 186
Josh
  • 13,530
  • 29
  • 114
  • 159
  • 1
    Is there anything specific you don't understand in the JSR-120 or JSR-205 specification or in the example code provided in the JavaME SDK? – michael aubert May 18 '09 at 10:47
  • [Sending and Receiving SMS on J2ME Device](http://www.java-tips.org/java-me-tips/midp/sending-receiving-sms-on-j2me-device-3.html) – TheTXI May 15 '09 at 18:50

2 Answers2

4

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"
thejartender
  • 9,339
  • 6
  • 34
  • 51
Nilesh
  • 167
  • 3
  • 15
3

Start this thread to send SMS

public class SendSMS extends Thread {

private String receiver;
private String receivedMsg;
private HomeScreen home;
private boolean bool = false;
private boolean notsent;

public SendSMS(HomeScreen gen, String msg, String number) {
    this.home = gen;
    this.receiver = number;
    this.receivedMsg = msg;
}

public void run() {
    while (!bool) {
        SendMessage();
    }
}

/**
 * Send the mesage using WMA api.
 */
private void SendMessage() { 
    String s = "sms://" + receiver;
    send(s);
}

private void send(String url) {
    MessageConnection messageconnection = null;
    try {
        messageconnection = (MessageConnection) Connector.open(url);
        TextMessage textmessage = (TextMessage) messageconnection.newMessage(MessageConnection.TEXT_MESSAGE);
        textmessage.setAddress(url);
        textmessage.setPayloadText(receivedMsg);
        messageconnection.send(textmessage);
    } catch (Exception throwable) {
        notsent = true;
        home.genericObject.setSmsStatus(false);
        if (!home.isNokia()) {
            new PopUp("Message not sent"); // not sent
        }
        bool = true;
        try {
            messageconnection.close();
        } catch (Exception e) {
        }
    }

    if (messageconnection != null) {
        try {
            messageconnection.close();
            if (!notsent) {
                home.genericObject.setSmsStatus(false);
                if (!home.isNokia()) {
                    new PopUp("Message Sent"); // sent
                }
            }
            bool = true;
        } catch (Exception ie) {
            ie.printStackTrace();
        }
    }
  }
}

Nokia devices don't show system alert if message is sent from j2me. So if you want to show alert, then you have to create your own PopUp and show.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
abbas.aniefa
  • 2,855
  • 21
  • 30