在nokia 6600上测试可发送短信^-^

public class sendthread extends thread {
private string phone;


private string content;



public sendthread(string phone, string content) {
this.phone = phone;
this.content = content;
}


public void run() {
try {
string addr = "sms://" + phone;
//system.out.println("发送地址为:" + addr);
messageconnection conn = (messageconnection) connector.open(addr);
textmessage msg = (textmessage) conn.newmessage(messageconnection.text_message);
msg.setpayloadtext(content);
conn.send(msg);
conn.close();
} catch (exception e) {
//system.out.println("error in sending");
e.printstacktrace();
}
}
}


 


第二种j2me发送短信的方法,nokia 6600实机测试通过:-)


import javax.microedition.io.connector;
import javax.microedition.lcdui.alert;
import javax.microedition.lcdui.alerttype;
import javax.microedition.lcdui.command;
import javax.microedition.lcdui.commandlistener;
import javax.microedition.lcdui.display;
import javax.microedition.lcdui.displayable;
import javax.microedition.lcdui.form;
import javax.microedition.lcdui.textfield;
import javax.microedition.midlet.midlet;
import javax.microedition.midlet.midletstatechangeexception;
import javax.wireless.messaging.message;
import javax.wireless.messaging.messageconnection;
import javax.wireless.messaging.textmessage;


public class sendnotemethod2 extends midlet implements commandlistener {
private display dis;


private command send;



private string serverport;



private form f;



private textfield tf1, tf2;



private boolean done;



private messageconnection sconn;



public sendnotemethod2() {
super();
dis = display.getdisplay(this);
f = new form("短信发送测试");
send = new command("发送", command.ok, 0);
tf1 = new textfield("电话:", null, 255, textfield.phonenumber);
tf2 = new textfield("内容:", null, 255, textfield.any);
f.append(tf1);
f.append(tf2);
f.addcommand(send);
f.setcommandlistener(this);
dis.setcurrent(f);
serverport = "5000";
}


protected void startapp() throws midletstatechangeexception {
try {
sconn = (messageconnection) connector.open("sms://:" + serverport);
done = false;
new thread(new smsserver()).start();
} catch (exception e) {
system.out.print("error in start/n");
e.printstacktrace();
}
}


protected void pauseapp() {
done = true;
try {
sconn.close();
} catch (exception e) {
system.out.print("error in pause");
e.printstacktrace();
}


}



protected void destroyapp(boolean arg0) throws midletstatechangeexception {
done = true;
try {
sconn.close();
} catch (exception e) {
system.out.print("error in pause");
e.printstacktrace();
}
}


public void commandaction(command c, displayable d) {
if (c == send) {
try {


thread send2thread = new thread() {
public void run() {
system.out.println("try send2command.....");


try {
string addr = "sms://" + tf1.getstring();
system.out.println("发送地址为:" + addr);
textmessage msg = (textmessage) sconn.newmessage(messageconnection.text_message);
msg.setaddress(addr);
msg.setpayloadtext(tf2.getstring());
sconn.send(msg);
} catch (exception exc) {
exc.printstacktrace();
}
}
};
send2thread.start();
} catch (exception e) {
system.out.println("error in sending");
e.printstacktrace();
}
}
}


class smsserver implements runnable {
public void run() {
try {
while (!done) {
message msg = sconn.receive();
if (msg instanceof textmessage) {
textmessage tmsg = (textmessage) msg;
string msgtext = tmsg.getpayloadtext();


// construct the return message
textmessage rmsg = (textmessage) sconn
.newmessage(messageconnection.text_message);
rmsg.setaddress(tmsg.getaddress());
rmsg.setpayloadtext("message " + msgtext
+ " is received");
sconn.send(rmsg);


alert alert = new alert("received", msgtext, null,
alerttype.error);
alert.settimeout(alert.forever);
dis.setcurrent(alert);
} else {
throw new exception("received is not a text mesg");
}
}
} catch (exception e) {
system.out.println("error in server receiving");
e.printstacktrace();
}
}
}
}