一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java实现短信回复

时间:2022-07-02 17:41:57 编辑:袖梨 来源:一聚教程网

 程序员:    杨星辉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;

    }

}

热门栏目