How to send text SMS in Java ME
Article Metadata
Code Example
Source file: Media:HowToSendTextSMSMIDlet.zip
Article
Created: SergioEstevao
(15 Nov 2007)
Last edited: hamishwillee
(29 Aug 2012)
WMAPI allows Java ME applications to access messaging functionalities, as sending and receiving SMS and MMS messages. This article explains how to use it to send a simple text message.
Contents |
Overview
The following Java ME tip explains a method of sending text messages using WMAPI. In the program TextMessage interface represents a text message. The setPayloadText() method sets the characters in the message.
public boolean sendSms(String number, String message){
boolean result = true;
try {
//sets address to send message
String addr = "sms://"+number;
// opens connection
MessageConnection conn = (MessageConnection) Connector.open(addr);
// prepares text message
TextMessage msg =
(TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
//set text
msg.setPayloadText(message);
// send message
conn.send(msg);
conn.close();
} catch (SecurityException se) {
// probably the user has not allowed to send sms
// you may want to handle this differently
result = false;
} catch (Exception e) {
result = false;
}
return result;
}
Download
You can download a sample MIDlet showing the code presented in this article here: Media:HowToSendTextSMSMIDlet.zip
Notes
- Remember that a single message has a maximum size (140 bytes, which translates to 160 7-bit characters). If your message is too long to fit in one message it can be splitted to a maximum of three SMS, this is handled automatic by the WMA implementation.
- Potentially-blocking operations, like sending a text message, should always be performed in separate threads, to avoid blocking the main MIDlet thread.



19 Sep
2009
This article provides a simple code example (and downloadable sample Midlet) for sending an SMS using Java ME. The process of sending an SMS is fairly simple, and should be easily implemented by even beginners. The article shows how sending an sms comprises the following steps: 1. Opening a new Message connection consisting of "sms://" followed by the intended recipient's phone number. 2. Creating the TextMessage object. 3. Setting the "payload text" of the message (the contents of the message). 4. Sending the message using the MessageConnection object. 5. Closing the MessageConnection.
The example shows clearly how to send an SMS using Java ME. It also points out possible problems - such as exceeding the maximum allowed length and the need to send messages using a separate thread to ensure that interaction can continue.
Lakshmishar - How to Receive message from Java ME
Thanks a lot for nokia which I am able to send message from the above the code.
But i have tried in write a code and googled bt i am not able to get code for receiving message code.
It will be more helpful if some one provide a working code.
Thanks in advance.lakshmishar 08:42, 10 November 2011 (EET)