smslib短信猫发短信
硬件是短信猫一个,COM口用超级终端可以发送成功及用相关软件也能发送成功。可在放在myEclipse中出错。已做了环境的配置,在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下,具体代码如下:
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(); srv = new Service(); SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 9600, "wavecom", "17254");//115200是波特率,一般为9600。可以通过超级终端测试出来 gateway.setInbound(true); gateway.setOutbound(true); gateway.setSimPin("0000"); gateway.setOutboundNotification(outboundNotification); srv.addGateway(gateway); srv.startService(); msg = new OutboundMessage("13418687205", "这个是用java发的中文短信!"); msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的 srv.sendMessage(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(); } }}
org.smslib.GatewayException: Comm library exception: java.lang.reflect.InvocationTargetException at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:93) at org.smslib.modem.AModemDriver.connect(AModemDriver.java:106) at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:111) at org.smslib.Service$1Starter.run(Service.java:227)
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();
// Read Messages. The reading is done via the Service object and
// affects all Gateway objects defined. This can also be more directed to a specific
// Gateway - look the JavaDocs for information on the Service method calls.
msgList = new ArrayList<InboundMessage>();
this.srv.readMessages(msgList, MessageClasses.ALL);
for (InboundMessage msg : msgList)
System.out.println(msg);
// Sleep now. Emulate real world situation and give a chance to the notifications
// methods to be called in the event of message or voice call reception.
System.out.println("Now Sleeping - Hit <enter> to terminate.");
System.in.read();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
this.srv.stopService();
}
}
public class InboundNotification implements IInboundMessageNotification
{
public void process(String gatewayId, MessageTypes msgType, InboundMessage msg)
{
if (msgType == MessageTypes.INBOUND) System.out.println(">>> New Inbound message detected from Gateway: " + gatewayId);
else if (msgType == MessageTypes.STATUSREPORT) System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gatewayId);
System.out.println(msg);
try
{
// Uncomment following line if you wish to delete the message upon arrival.
// ReadMessages.this.srv.deleteMessage(msg);
}
catch (Exception e)
{
System.out.println("Oops!!! Something gone bad...");
e.printStackTrace();
}
}
}
public class CallNotification implements ICallNotification
{
public void process(String gatewayId, String callerId)
{
System.out.println(">>> New call detected from Gateway: " + gatewayId + " : " + callerId);
}
}
public class GatewayStatusNotification implements IGatewayStatusNotification
{
public void process(String gatewayId, GatewayStatuses oldStatus, GatewayStatuses newStatus)
{
System.out.println(">>> Gateway Status change for " + gatewayId + ", OLD: " + oldStatus + " -> NEW: " + newStatus);
}
}
public static void main(String args[])
{
ReadMessages app = new ReadMessages();
try
{
app.doIt();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
[解决办法]
这个问题需要注意下 jdk 的版本, 我使用的是 jdk 1.5.0.6 , 没有出现那样的问题了。
[解决办法]
支持楼主!
[解决办法]
你本机和服务器上的系统是不是一样的撒?
[解决办法]
在你的 工程lib 下加入 commons-net-1.4.1.jar , 你是加在 D:\Program Files\Java\jdk1.5.0_04\lib\commons-net-1.4.1.jar ,你是加在
jdk 目录下的, 如果还是不行话参照下列条件:
To use SMSLib, you should install the following components:
SUN JDK 1.5 or newer.
Java Communications Library.
Apache ANT for building the sources.
Simple logging facade for Java (slf4j).
Apache Jakarta Commons - NET.
官方用法路径:http://code.google.com/p/smslib/wiki/Installation
相信楼主应该很快就可以解决, 注意几个重要 jar 包的用法, 在上面路径上已经说的很详细了, 其实只需要注意 Apache Jakarta Commons - NET. 的用法就可以了, 我没按照官方的做法来做。 呵呵..... Good Luck!!
[解决办法]
估计90%是comm.jar所对应的动态链节库win32comm.dll和properties文件没放对,
文件javax.comm. properties拷贝到%JAVA_HOME%\jre\lib;
文件win32comm.dll拷贝到%JAVA_HOME%\bin。
注意%JAVA_HOME%是jdk的路径,而非jre。