Showing posts with label Sending SMS using J2ME. Show all posts
Showing posts with label Sending SMS using J2ME. Show all posts

Friday, April 1, 2011

How to send message to mobile using SMTP

Following code will send SMS to any phone number using your Way2SMS account


import java.rmi.RemoteException;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;
import java.lang.Integer;
import java.io.*;
import javax.microedition.io.*;

/**
* @author kiLLer
*/
public class Midlet2 extends MIDlet implements CommandListener {
private Command exitCommand; // The exit command
private Command okCommand; // The exit command
private Command cancelCommand; // The exit command
private Command nextCommand; // The exit command
private Command backCommand; // The exit command
private Display display; // The display for this MIDlet

public Midlet2() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.EXIT, 2);
}
public void startApp() {
String rslt = null;
boolean isCalled = false;

sendsms("http://ubaid.tk/sms/sms.aspx?uid=Your_Phone_Number&pwd=Your_Way2SMS_Password&msg=TESTING IS DONE 5525&phone=9920965650&provider=way2sms");
int rn = RandomNo();
String rndm = Integer.toString(rn);
TextBox t = new TextBox("Hello", "......"+rndm, 256, 0);
t.addCommand(exitCommand);
t.setCommandListener(this);

display.setCurrent(t);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
throw new UnsupportedOperationException("Not supported yet.");
}
}

public int RandomNo()
{
Random r=new Random(System.currentTimeMillis());
int rno= Math.abs(r.nextInt());
return (1000 + rno % 7000);
}

public void sendsms(String url)
{
HttpConnection connection = null;
InputStream inputstream = null;
try {
connection = (HttpConnection) Connector.open(url);
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("Content-Type","//text plain");
connection.setRequestProperty("Connection", "close");
} catch (Exception ex) {}
}
}

You have to make following changes in sendsms function

  • change uid to your phone number
  • change pwd to your Way2sms account password
  • change msg to message you want send to the user
  • change phone to receivers phone number
After calling sendsms function you will one of the following two replies
  • 1 : SMS sent
  • 2 : some error
This code somehow won't work for BPL phone numbers so use different network provider