使用java操作wavecom来发送短信


 实现这个功能,需要两个jar包,可以到http://code.google.com/p/smslib下载smslib-v3.3.0-B2-bin.zip和javacomm20-win32.zip。

 首先,把smslib-v3.3.0-B2-bin.zip解开,在smslib\dist\lib目录下找到smslib-3.3.0b2.jar,放入工程lib中,再把javacomm20-win32.zip解开,里面的comm.jar需要放到工程lib下,javax.comm.properties放到%JAVA_HOME%/jre/lib下,win32com.dll放到%JAVA_HOME%/jre/bin下。路径放错了,调用起来就会报错的。
环境配置好了以后,使用起来很简单,贴下我的代码:

 

package com.cn;
import org.smslib.AGateway;
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() throws Exception
{
   Service srv;
   OutboundMessage msg;
   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();
  
   SerialModemGateway gateway = new SerialModemGateway("modem.com17", "COM17", 9600, "wavecom", "");//com名称,串口号,破特率,连接设备名称,设备型号
   gateway.setInbound(true);//设置网关可以写入信息 
   gateway.setOutbound(true);//设置网关可以读入信息
   gateway.setSimPin("0000");
   //gateway.setSmscNumber("555555");
   gateway.setOutboundNotification(outboundNotification);
   srv.addGateway(gateway);//添加网光
   System.out.println("初始化成功,准备开启服务");
   srv.startService();//初始化网关
   msg = new OutboundMessage("13426478376", "这个是用java发的中文短信放松放松dfgdfsdf的!");//手机号码,和短信内容
   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();
   }
   catch (Exception e)
   {
    e.printStackTrace();
   }
}
}
 

 

 

你可能感兴趣的:(java)