使用java操作wavecom短信猫来发短信的方法
实现这个功能,需要两个jar包,可以到http://download.csdn.net/detail/yuexin2/6855961下载smslib-3.3.0b2.jar和http://download.csdn.net/detail/yuexin2/6855979下载javacomm20-win32.zip。
首先,把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下。路径放错了,调用起来就会报错的。注意在官方网站的jar包已经不能运行下面代码。移动测试没问题,其他的暂时没测试。
环境配置好了以后,使用起来很简单,贴下我的代码:
package song.test;
importorg.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;
//移动ok
public class SendMessage {
public class OutboundNotification implementsIOutboundMessageNotification {
public void process(String gatewayId, OutboundMessage msg) {
System.out.println("Outbound handler called from Gateway: "
+ gatewayId);
System.out.println(msg);
}
}
@SuppressWarnings("deprecation")
public void sendSMS(String mobilePhones,String content) {
Service srv;
OutboundMessage msg;
OutboundNotification outboundNotification = new OutboundNotification();
srv= new Service();
SerialModemGateway gateway = newSerialModemGateway("modem.com3",
"COM3", 9600, "wavecom", ""); //设置端口与波特率
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
srv.addGateway(gateway);
System.out.println("初始化成功,准备开启服务");
try{
srv.startService();
System.out.println("服务启动成功");
String[] phones = mobilePhones.split(",");
for (int i = 0; i < phones.length; i++) {
msg = new OutboundMessage(phones[i], content);
msg.setEncoding(MessageEncodings.ENCUCS2); //中文
srv.sendMessage(msg);
}
srv.stopService();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SendMessage sendMessage = new SendMessage();
sendMessage.sendSMS("183XXXX7887", "您要发送的内容!");
}
}