views:

148

answers:

2

I am trying to send a text message to a phone and I get an error

Fail to send because of unknown reason. -java.io.IOException

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class Midlet extends MIDlet {
    Form form = new Form("Form");
    Display display;
    public void startApp()
    {
        display = Display.getDisplay(this);
        display.setCurrent(form);

        sendSMS("Hello from j2me");
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    private void sendSMS(String s) {
        String destination = "+12234567890";
        String addr = "sms://" + destination;

        out("Setting up message");
        MessageConnection sender = null;

        try 
        {
            try 
            {
                    sender = (MessageConnection) Connector.open(addr);
                    TextMessage msg = (TextMessage) sender.newMessage(MessageConnection.TEXT_MESSAGE);
                    msg.setPayloadText(s);
                    out("sending");
                    sender.send(msg);
                    out("sent successfully");
            } 
            catch (Exception ex) 
            {
                    out("Error1:" + ex.getMessage() + " : " + ex.toString() + "\n\n");
            } 
            finally 
            {
                sender.close();
            }
        } 
        catch (Exception ex) {
                //handle exception
                out("Error2:" + ex.getMessage() + " : " + ex.toString() + "\n\n");
        }
    }

    private void out(String str)
    {
        form.append(str + "\n");
    }
}
A: 

Did you add permissions to your jad?

MIDlet-Permissions: javax.microedition.io.Connector.sms,javax.wireless.messaging.sms.send

Hanan Bercu
A: 

All sorts of reasons:

  • No credit on PAYG phone
  • No mobile reception
  • SMS API blocked by handset operator
  • User rejected security prompt (this would cause a SecurityException)
  • Invalid mobile number
funkybro