欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

smslib发短信实例代码(电脑发短信)

程序员文章站 2023-12-22 08:44:40
smslib是一个由很多程序员共同开发的,用于支持gsm猫或者手机发送短信的开源项目,下面来个实例代码 复制代码 代码如下:import java.util.arrayl...

smslib是一个由很多程序员共同开发的,用于支持gsm猫或者手机发送短信的开源项目,下面来个实例代码

复制代码 代码如下:

import java.util.arraylist;
import java.util.list;

import org.apache.log4j.logger;
import org.smslib.icallnotification;
import org.smslib.iinboundmessagenotification;
import org.smslib.ioutboundmessagenotification;
import org.smslib.inboundmessage;
import org.smslib.inboundmessage.messageclasses;
import org.smslib.library;
import org.smslib.message.messageencodings;
import org.smslib.message.messagetypes;
import org.smslib.outboundmessage;
import org.smslib.service;
import org.smslib.modem.serialmodemgateway;

/**
 * @author terry
 *
 */
public class smsmodem {

 // 短信网关
 private serialmodemgateway gateway = null;
 java.util.resourcebundle rb = null;//resourcebundle.getbundle("sms");
 static smsmodem smsmodem = null;
 outboundnotification outboundnotification = new outboundnotification();
 private static final logger log = logger.getlogger(smsmodem.class);
 service srv;
 inboundnotification inboundnotification = new inboundnotification();
 // create the notification callback method for inbound voice calls.
 callnotification callnotification = new callnotification();

 public smsmodem() {
  try {
   //readmessages rm = new readmessages();
   //rm.doit();

   rb = resourcebundle.getbundle("sms");
   string portname= "com10";
   int port = 9600;
   log.info("default portname:" + portname);
   log.info("default port:" + port);
   if(rb != null)
   {
    log.info("rb is not null");
    if(rb.getstring("smsport") != null && !"".equals(rb.getstring("smsport")))
    {
     portname = rb.getstring("smsport");
     log.info("portname:" + portname);
    }
    if(rb.getstring("smsbolv") != null && !"".equals(rb.getstring("smsbolv")))
    {
     port = integer.valueof(rb.getstring("smsbolv"));
     log.info("port:" + port);
    }
   }
   // 初始化短信网关
   gateway = new serialmodemgateway("modem." + portname, portname, port,
     "wavecom", "17254");

  } catch (exception e) {
   log.error("网关初始化失败:" + e.getmessage());
   e.printstacktrace();
  }
 }

 public static smsmodem getinstant() {
  if (smsmodem == null) {
   smsmodem = new smsmodem();
  }
  return smsmodem;
 }

 public serialmodemgateway getgateway() {
  return gateway;
 }

 public void sendmessage(string phone, string content) throws exception {
  doit(phone, content);
 }

 /**
  * 发送短信
  * @param phone
  * @param content
  * @throws exception
  */
 public void doit(string phone, string content) throws exception {

  outboundmessage msg;

  log.info("sent example: send message from a serial gsm modem.");
  log.info(library.getlibrarydescription());
  log.info("sent version: " + library.getlibraryversion());
  if (srv == null) {
   srv = new service();
   srv.s.serial_polling = true;
   gateway.setinbound(true);
   gateway.setoutbound(true);
   gateway.setsimpin("0000");
   gateway.setoutboundnotification(outboundnotification);
   gateway.setinboundnotification(inboundnotification);
   gateway.setcallnotification(callnotification);
   srv.addgateway(gateway);
   srv.startservice();
  }

  if (gateway != null) {
   log.info("sent modem information:");
   log.info("sent  manufacturer: " + gateway.getmanufacturer());
   log.info("sent  model: " + gateway.getmodel());
   log.info("sent  serial no: " + gateway.getserialno());
   log.info("sent  sim imsi: " + gateway.getimsi());
   log.info("sent  signal level: " + gateway.getsignallevel() + "%");
   log.info("sent  battery level: " + gateway.getbatterylevel() + "%");
  }
  // send a message synchronously.

  msg = new outboundmessage(phone, content);
  msg.setencoding(messageencodings.encucs2);// 这句话是发中文短信必须的
  srv.sendmessage(msg);
 }

 /**
  * 发送消息类
  * @author terry
  *
  */
 public class outboundnotification implements ioutboundmessagenotification {
  public void process(string gatewayid, outboundmessage msg) {
   log.info("sent outbound handler called from gateway: " + gatewayid);
   log.info(msg);
  }
 }
 //接收消息类
 public string readmessage()
 {
  stringbuffer sb = new stringbuffer("");
  list<inboundmessage> msglist;
  // create the notification callback method for inbound & status report
  // messages.

  try
  {
   system.out.println("read example: read messages from a serial gsm modem.");
   system.out.println(library.getlibrarydescription());
   system.out.println("read version: " + library.getlibraryversion());
   // create new service object - the parent of all and the main interface
   // to you.
   if (srv == null) {
    srv = new service();
    srv.s.serial_polling = true;
    gateway.setinbound(true);
    gateway.setoutbound(true);
    gateway.setsimpin("0000");
    gateway.setoutboundnotification(outboundnotification);
    gateway.setinboundnotification(inboundnotification);
    gateway.setcallnotification(callnotification);
    srv.addgateway(gateway);
    srv.startservice();
   }

   
   // similarly, you may define as many gateway objects, representing
   // various gsm modems, add them in the service object and control all of them.
   //
   // start! (i.e. connect to all defined gateways)

   log.info("read modem information:");
   log.info("read   manufacturer: " + gateway.getmanufacturer());
   log.info("read   model: " + gateway.getmodel());
   log.info("read   serial no: " + gateway.getserialno());
   log.info("read   sim imsi: " + gateway.getimsi());
   log.info("read   signal level: " + gateway.getsignallevel() + "%");
   log.info("read   battery level: " + gateway.getbatterylevel() + "%");
   // read messages. the reading is done via the service object and
   // affects all gateway objects defined. this can also be more directed to a specific
   // gateway - look the javadocs for information on the service method calls.
   msglist = new arraylist<inboundmessage>();
   this.srv.readmessages(msglist, messageclasses.all);
   int num = 1;
   for (inboundmessage msg : msglist)
   {
    sb.append("第" + num + "条;发件人:"+msg.getoriginator() + ";内容:" + msg.gettext() + "\n");
    //sb.append(msg.tostring() + "\n");
    log.info("第" + num + "条;发件人:"+msg.getoriginator() + ";内容:" + msg.gettext() + "\n");
    num++;
    log.info(msg);
   }
   // sleep now. emulate real world situation and give a chance to the notifications
   // methods to be called in the event of message or voice call reception.
   //system.out.println("now sleeping - hit <enter> to terminate.");
   //system.in.read();
  }
  catch (exception e)
  {
   sb.append(e.getmessage());
   e.printstacktrace();
  }
  finally
  {
   //this.srv.stopservice();
  }
  return sb.tostring();
 }

 public class inboundnotification implements iinboundmessagenotification
 {
  public void process(string gatewayid, messagetypes msgtype, inboundmessage msg)
  {
   if (msgtype == messagetypes.inbound) system.out.println(">>> new inbound message detected from gateway: " + gatewayid);
   else if (msgtype == messagetypes.statusreport) system.out.println(">>> new inbound status report message detected from gateway: " + gatewayid);
   system.out.println(msg);
   try
   {
    // uncomment following line if you wish to delete the message upon arrival.
    // srv.deletemessage(msg);
   }
   catch (exception e)
   {
    system.out.println("oops!!! something gone bad...");
    e.printstacktrace();
   }
  }
 }

 public class callnotification implements icallnotification
 {
  public void process(string gatewayid, string callerid)
  {
   system.out.println(">>> new call detected from gateway: " + gatewayid + " : " + callerid);
  }
 }

}

上一篇:

下一篇: