java实现短信回复

作者:袖梨 2022-07-02

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

    }

}

相关文章

精彩推荐