使用SMSLib發短信(轉載)
1.mysql做后臺數據庫;
2.java應用smslib實現短信的發送與接收。
3.可以考慮在linux下實現;
SMSLib是一個API庫用于通過GSM Modem收發SMS短信息
SMSLib是一個API庫用于通過GSM Modem收發SMS短信息。提供Java和.Net兩個版本。
http://smslib.sourceforge.net/
在Windows環境下使用SMSLib編程的時候,我們需要做一下comm的配置:
1. 將win32com.dll放置在%JAVA_HOME%\jre\bin下
2. 將comm.jar放置在%JAVA_HOME%\jre\lib\ext下
3. 將javax.comm.properties放置在%JAVA_HOME%\jar\lib下
再試試SMSLib自帶的examples,看看效果。
使用SMSLib發短信
最近的項目,有個需求是要發短信,設備是一臺WAVECOM的串口短信貓。
隨貓附送的接口是ActiveX控件做的,我本來打算用VB寫個接口程序來調用,但是看了下它的Demo例子,要發送的消息要放到控件的數組,發送成功以后還要自己刪,相當麻煩。
調用短信貓,都是通過串口通訊和AT命令,其實那個控件也不過是做了一下封裝。在Java環境下,實現這些也是可以的,但是發中文短信要采用PDU模式,文字要經過PDU編碼,去看文檔也很麻煩。還好這些事情網上的開源項目SMSLib已經都已經幫我們做了。
到http://co
其中smslib-v3.1.0.zip是源代碼,需要用ant編譯成jar,javacomm20-win32.zip下是串行通訊協議需要的包。
javacomm20- win32.zip解開,里面的comm.jar需要放到%JAVA_HOME%/jre/lib/ext 下,javax.comm.properties放到%JAVA_HOME%/jre/lib下,win32com.dll放到%JAVA_HOME% /jre/bin下。路徑放錯了,調用起來就會報錯的。
實際編譯的時候,還需要一個commons-net的包,我下的是commons-net-1.4.1.jar。
環境配置復雜了點,但是用起來很簡單,貼一下我的代碼。
public boolean open() {
srv = new Service();
//comPort 串口名,比如COM1或者/dev/ttyS1
//baudRate 端口速度,WAVECOM是9600
//manufacturer,model 制造商和型號隨便填
SerialModemGateway gateway = new SerialModemGateway("SMS",comPort,baudRate,manufacturer,model,srv.getLogger());
gateway.setInbound(true);
gateway.setOutbound(true);
//gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
srv.addGateway(gateway);
try {
srv.startService();
}
catch (Exception ex) {
log.error(ex);
return false;
}
return true;
}
public boolean sendSms(String mobile,String content) {
msg = new OutboundMessage(mobile, content);
msg.setEncoding(MessageEncodings.ENCUCS2);
try {
srv.sendMessage(msg);
System.out.println(msg);
}
catch (Exception ex) {
log.error(ex);
return false;
}
return true;
}
public void close() {
try {
srv.stopService();
}
catch (Exception ex) {
log.error(ex);
}
}
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);
}
}
另外附,通過遠程終端按文本方式發短信的AT命令
AT+CMGF=1
OK
AT+CMGS=136XXXXXXX
>Test^Z
+CMGS: 204
OK
--------------------------------------------------------
JAVA短信收發控件開發示例
package com.diagcn.smslib.test;
imp
imp
imp
imp
imp
imp
imp
public class SampleClass {
/**
* @param args
*/
public static void main(String[] args) {
// 與短信設備建立連接,參數 1、端口號,2、速率,3、短信設備牌子,4、短信設備型號
CService srv = new CService("COM1", 9600, "Wavecom", "");
try {
// 設置短信中心號碼
srv.setSmscNumber("+8613800210500");
// 連接設備
srv.connect();
// 連接成功,可以顯示短信設備狀態
System.out.println("Mobile Device Information: ");
System.out.println(" Manufacturer : "
+ srv.getDeviceInfo().getManufacturer());
System.out.println(" Model : "
+ srv.getDeviceInfo().getModel());
System.out.println(" Serial No : "
+ srv.getDeviceInfo().getSerialNo());
System.out.println(" IMSI : "
+ srv.getDeviceInfo().getImsi());
System.out.println(" S/W Version : "
+ srv.getDeviceInfo().getSwVersion());
System.out.println(" Battery Level : "
+ srv.getDeviceInfo().getBatteryLevel() + "%");
System.out.println(" Signal Level : "
+ srv.getDeviceInfo().getSignalLevel() + "%");
// 創建發送對象
COutgoingMessage msg = new COutgoingMessage("13917074111", "中文測試");
// 設置編碼
msg.setMessageEncoding(CMessage.MessageEncoding.EncUcs2);
// 此短信需要狀態回復
msg.setStatusReport(true);
// 短信有效期
msg.setValidityPeriod(8);
// 發送短信
srv.sendMessage(msg);
// 接收短信代碼====================================================================
List<CIncomingMessage> msgList = new LinkedList<CIncomingMessage>();
srv.readMessages(msgList, CIncomingMessage.MessageClass.All);
for (int i = 0; i < msgList.size(); i++) {
CIncomingMessage message = msgList.get(i);
if (message instanceof CStatusReportMessage) {
// 此短消息為 狀態回復短消息
}
srv.deleteMessage(message); // 刪除都到的短信
}
// ==============================================================================
srv.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}}
----------------------------------
使用java操作wavecom短信貓來發短信的方法
2008年07月29日 星期二 09:58
由于業務的需要,今天用java實現了用wavecom短信貓發短信的功能,本來這個應該用隨貓購買的二次開發接口實現的,但由于這幾臺貓買的時候,經銷商沒有提供二次開發接口,所以我不得不在網上找了資料,自己寫了個接口實現了發短信的功能。
實現這個功能,需要兩個jar包,可以到http://co
首先,把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下。路徑放錯了,調用起來就會報錯的。
環境配置好了以后,使用起來很簡單,貼下我的代碼:
// SendMessage.java - Sample application.
//
// This application shows you the basic procedure for sending messages.
// You will find how to send synchronous and asynchronous messages.
//
// For asynchronous dispatch, the example application sets a callback
// notification, to see what's happened with messages.
package song.test;
imp
imp
imp
imp
imp
imp
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.com1", "COM1", 115200, "wavecom", "17254");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
srv.addGateway(gateway);
srv.startService();
System.out.println("Modem Information:");
System.out.println(" Manufacturer: " + gateway.getManufacturer());
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();
// Send a message synchronously.
msg = new OutboundMessage("13649251175", "這個是用java發的中文短信!");//手機號碼,和短信內容
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();
}
}
}