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

java调用短信猫发短信示例

程序员文章站 2024-02-26 19:23:40
具体的操作步骤如下:1、把smslib-3.3.0b2.jar、comm.jar与log4j-1.2.11.jar,放入到工程的lib中;2、把javax.comm.pro...

具体的操作步骤如下:
1、把smslib-3.3.0b2.jar、comm.jar与log4j-1.2.11.jar,放入到工程的lib中;
2、把javax.comm.properties放到%java_home%/jre/lib下;
3、把win32com.dll放到%java_home%/jre/bin下;
4  把comm.jar放到%java_home%/jre/ext下
注意:路径放错,调用起来就会报错;jdk的版本,用的版本是jdk-1_5_0_04。

复制代码 代码如下:

ackage com.alonely.notecat;
import org.smslib.ioutboundmessagenotification;
import org.smslib.outbou、ndmessage;
import org.smslib.service;
import org.smslib.message.messageencodings;
import org.smslib.modem.serialmodemgateway;

public class sendmessage {
 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);
  }
 }
 @suppresswarnings("deprecation")
 public void sendsms(string mobilephones, string content) {
  service srv;
  outboundmessage msg;
  outboundnotification outboundnotification = new outboundnotification();
  srv = new service();
  serialmodemgateway gateway = new serialmodemgateway("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("您要发送的手机号", "您要发送的内容!");
 }
}