[转载]用Java调用短信猫

it2022-05-08  9

原文地址:用Java调用短信猫 作者:boshding

今天我给大家介绍的是,用Java调用短信猫实现为手机发短息的功能。其实这样的例子网上已经有很多了,但是我觉得网上的例子中对发短息异常的情况分析的不够透彻,所以我这里也发一个Java调用短信猫的小例子。

具体的操作步骤如下:1、smslib-3.3.3.jar、comm.jar与log4j-1.2.13.jar,放入到工程的lib中;2、把javax.comm.properties放到%JAVA_HOME%/jre/lib下;3、把win32com.dll放到%JAVA_HOME%/jre/bin下;注意:路径放错了,调用起来就会报错的;还有一点就是JDK的版本,我用的版本是jdk-1_5_0_04。

这样配置好了,运行我下面的代码就可以发短息了,具体的代码如下:

package com.alonely.notecat; import org.smslib.IOutboundMessageNotification; import org.smslib.OutboundMessage; 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("您要发送的手机号", "您要发送的内容!");  } } Ok,我代码贴完了,下面告诉大家我遇到的异常,并且是如何解决的: 1、如果你报了如下错误: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger 那是因为你没有把log4j-1.2.13.jar倒入到工程中。 2、如果你报了如下错误:

org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException:javax.comm.NoSuchPortException

说明你的Eclipse选择的JDK有问题,反正我用的是jdk-1_5_0_04版本,没有任何问题;我在MyEclipse5.5下用它自带的JDK就有问题,这点请大家注意。 转自: http://ks2144634.blog.163.com/blog/static/133585503201092962726126/

转载于:https://www.cnblogs.com/liuzhuqing/archive/2011/06/25/7481058.html


最新回复(0)