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

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);
  }
 }

}

(0)

相关推荐

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

    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.IOutbo

  • Android Studio Intent隐式启动,发短信,拨号,打电话,访问网页等实例代码

    Android Studio Intent隐式启动,发短信,拨号,打电话,访问网页等实例代码 功能 创建5个按钮,隐式启动.发短信.拨号按钮.电话按钮.打开网页按钮.通过使用Intent来完成各自按钮下的功能 代码目录如下 详细代码如下: activity_main.xml代码如下 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://sc

  • Springboot实现Java阿里短信发送代码实例

    阿里云短信服务还是非常好的,接口稳定,同时还提供SDK,使得企业的接入该服务更加方便.下面来看看阿里云提供的发短信的java实例代码吧 1.接口TestController import java.util.Random; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import

  • java 实现发短信功能---腾讯云短信

    前言 如今发短信功能已经成为互联网公司的标配,本篇文章将一步步实现java发送短信 考察了许多提供短信服务的三方,几乎所有都需要企业认证才可以使用,这对于个人学习非常不方便.多方比较之后,选择了腾讯云(此处并非做广告),原因有两点: 支持微信公众号认证(门槛低)每月赠送100条免费短信(适合个人学习)(这一点请矿家子弟自行忽略) 开发环境 请参照: Spring Boot实现模块化的几种方法 腾讯云 ---短信 找到短信服务 开通之后,点击添加应用 设置短信签名(以公众号为例),签名类型选择公众

  • Android开发中通过手机号+短信验证码登录的实例代码

    首先,需要一个电话号码,目前很多账户都是将账户名设置成手机号,然后点击按钮获取手机验证码. 其次,你需要后台给你手机短信的验证接口,各个公司用的不一样,这个身为前端,不需要你来考虑,你只要让你后台给你写好接口,你直接调用就好了. activity_login.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.andr

  • 基于Java随机生成手机短信验证码的实例代码

    简单版 /** * 产生4位随机数(0000-9999) * * @return 4位随机数 */ public static String getFourRandom() { return StringUtils.leftPad(new Random().nextInt(10000) + "", 4, "0"); } 复杂版 /** * 创建指定数量的随机字符串 * @param numberFlag 是否是数字 * @param length * @return

  • java 随机生成6位短信验证码实例代码

    生成6位随机数字其实很简单,只需一行代码,具体如下: String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000); 具体实现发送短信验证码功能,以注册为例,其实现代码如下: 这里需要用到第三方短信验证码接口,这里用到的是乐信短信验证码接口,乐信接口API文档说明(http://www.lx598.com/apitext.html),乐信java调用短信验证码接口 demo(http://www.lx598.c

  • Java整合腾讯云短信发送实例代码

    目录 1. 引入相关maven依赖 2. 编写短信发送工具类 3. 业务层 3. 相关工具类 1. 引入相关maven依赖 <dependency> <groupId>com.github.qcloudsms</groupId> <artifactId>qcloudsms</artifactId> <version>1.0.6</version> </dependency> <dependency>

  • Django使用celery异步发送短信验证码代码示例

    目录 celery 1.celery介绍 1.1 celery应用举例 1.2 Celery有以下优点 1.3 Celery 特性 2.工作原理 2.1 Celery 扮演生产者和消费者的角色 3.异步发短信 1.settings同级目录下创建 celery 文件 2.配置settings文件 3 配置 settings同级目录下 init 文件 4.在utils下新建一个task.py文件 5.接口中调用 6 .先启动django项目 然后另开终端 cd到项目 celery 1.celery介

  • php短信接口代码

    本文实例为大家分享了几个常用的php短信接口代码,供大家参考,具体内容如下 1. 短信调用class     <?php /** * User: Administrator * Date: 2016/5/8 0008 * Time: 下午 2:36 */ class Sms{ //Luosimao api key private $_api_key = ''; private $_last_error = array(); private $_use_ssl = FALSE; private $

随机推荐