Java操作短信猫发短信

// SendMessage.java - Sample application.
//
// This application shows you the basic procedure for sending messages.
// You will find how to send synchronous and asynchronous messages.
//
// For asynchronous dispatch, the example application sets a callback
// notification, to see what's happened with messages.

package examples.modem;

import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;

public class SendMessage
{
    public void doIt(String PhoneNum,String Mesg) throws Exception
    {
        Service srv; //声明一个服务
        OutboundMessage msg; //OutboundMessage继承IOutboundMessageNotification接口,重写了process方法,用于网关设置回调函数
        OutboundNotification outboundNotification = new OutboundNotification();
        System.out.println("Example: Send message from a serial gsm modem.");
        System.out.println(Library.getLibraryDescription());//获取类库描述
        System.out.println("Version: " + Library.getLibraryVersion());//获取类库版本
        srv = new Service(); //初始化服务
        
        //115200是波特率,一般为9600。可以通过超级终端测试出来,声明网关 第一个参数为:网关ID,第二个是本机上短信猫的com口名称,第三是波特率,第四是短信猫生产厂商,第五设备的型号(可选)
        SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM7", 9600, "Wavecom", "17254");
        
        gateway.setInbound(true);//设置true,表示该网关可以接收短信,根据需求修改 
        gateway.setOutbound(true);//设置true,表示该网关可以发送短信,根据需求修改  
        gateway.setSimPin("0000");//sim卡锁,一般默认为0000或1234  
        gateway.setOutboundNotification(outboundNotification);//发送短信成功后的回调函方法  
        srv.addGateway(gateway);     //将网关添加到短信猫服务中 
        srv.startService();       //启动服务,进入短信发送就绪状态  
        System.out.println("Modem Information:");
        System.out.println("  Manufacturer: " + gateway.getManufacturer());
        System.out.println("  Model: " + gateway.getModel());
        System.out.println("  Serial No: " + gateway.getSerialNo());
        System.out.println("  SIM IMSI: " + gateway.getImsi());
        System.out.println("  Signal Level: " + gateway.getSignalLevel() + "%");
        System.out.println("  Battery Level: " + gateway.getBatteryLevel() + "%");
        System.out.println();
        // Send a message synchronously.
        
        msg = new OutboundMessage(PhoneNum, Mesg);
        msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的
        srv.sendMessage(msg);
        System.out.println(msg);
        System.out.println("Now Sleeping - Hit <enter> to terminate.");
        System.in.read();
        srv.stopService();
    }

    public class OutboundNotification implements IOutboundMessageNotification
    {
        public void process(String gatewayId, OutboundMessage msg)
        {
            System.out.println("Outbound handler called from Gateway: " + gatewayId);
            System.out.println(msg);
        }
    }

    public static void main(String args[])
    {
        SendMessage app = new SendMessage();
        try
        {
            app.doIt("13215243120","Hello,test content");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

需要两个jar包

首先,把smslib-3.3.0b2.jar和comm.jar,放入工程lib中,javax.comm.properties放到%JAVA_HOME%/jre/lib下,
win32com.dll放到%JAVA_HOME%/jre/bin下。路径放错了,调用起来就会报错的。
环境配置好了以后,把examples\modem下的SendMessage.java和ReadMessages.java拷贝到你的开发工具下

 

posted @ 2013-04-10 19:18  尼姑哪里跑  阅读(894)  评论(2编辑  收藏  举报