程序員: 楊星輝java實現短信回復
*
* 日 期: 2008-7-4
*
* 時 間: 下午05:24:07
*
* 短信發送類
*/
import javax.wireless.messaging.*;
import javax.microedition.io.*;
public class Sender {
public Sender() {
}
/* 傳入文字內容、電話號碼、端口號,進行發送短信,結果為true or false */
public static boolean send(String content, String phonenumber,
String smsPort) {
boolean result = true;
try {
String address = "sms://" + phonenumber + ":" + smsPort;
MessageConnection conn = (MessageConnection) Connector.open(address);
TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(content);
conn.send(msg);
conn.close();
} catch (Exception e) {
result = false;
e.getMessage();
}
return result;
}
/* 無端口 發送短信 */
public static boolean send(String content, String phonenumber) {
boolean result = true;
try {
String address = "sms://" + phonenumber;
MessageConnection conn = (MessageConnection) Connector.open(address);
TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(content);
conn.send(msg);
conn.close();
} catch (Exception e) {
result = false;
e.getMessage();
}
return result;
}
}