java使用smslib連接短信貓發(fā)送短信代碼分享
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.smslib.ICallNotification;
import org.smslib.IInboundMessageNotification;
import org.smslib.IOutboundMessageNotification;
import org.smslib.InboundMessage;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.Library;
import org.smslib.Message.MessageEncodings;
import org.smslib.Message.MessageTypes;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;
/**
* @author terry
*
*/
public class SmsModem {
// 短信網(wǎng)關(guān)
private SerialModemGateway gateway = null;
java.util.ResourceBundle rb = null;//ResourceBundle.getBundle("SMS");
static SmsModem smsModem = null;
OutboundNotification outboundNotification = new OutboundNotification();
private static final Logger LOG = Logger.getLogger(SmsModem.class);
Service srv;
InboundNotification inboundNotification = new InboundNotification();
// Create the notification callback method for inbound voice calls.
CallNotification callNotification = new CallNotification();
public SmsModem() {
try {
//ReadMessages rm = new ReadMessages();
//rm.doIt();
rb = ResourceBundle.getBundle("sms");
String portName= "COM10";
int port = 9600;
LOG.info("default portName:" + portName);
LOG.info("default port:" + port);
if(rb != null)
{
LOG.info("RB is not null");
if(rb.getString("smsport") != null && !"".equals(rb.getString("smsport")))
{
portName = rb.getString("smsport");
LOG.info("portName:" + portName);
}
if(rb.getString("smsbolv") != null && !"".equals(rb.getString("smsbolv")))
{
port = Integer.valueOf(rb.getString("smsbolv"));
LOG.info("port:" + port);
}
}
// 初始化短信網(wǎng)關(guān)
gateway = new SerialModemGateway("modem." + portName, portName, port,
"wavecom", "17254");
} catch (Exception e) {
LOG.error("網(wǎng)關(guān)初始化失?。? + e.getMessage());
e.printStackTrace();
}
}
public static SmsModem getInstant() {
if (smsModem == null) {
smsModem = new SmsModem();
}
return smsModem;
}
public SerialModemGateway getGateway() {
return gateway;
}
public void sendMessage(String phone, String content) throws Exception {
doIt(phone, content);
}
/**
* 發(fā)送短信
* @param phone
* @param content
* @throws Exception
*/
public void doIt(String phone, String content) throws Exception {
OutboundMessage msg;
LOG.info("Sent Example: Send message from a serial gsm modem.");
LOG.info(Library.getLibraryDescription());
LOG.info("Sent Version: " + Library.getLibraryVersion());
if (srv == null) {
srv = new Service();
srv.S.SERIAL_POLLING = true;
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
gateway.setInboundNotification(inboundNotification);
gateway.setCallNotification(callNotification);
srv.addGateway(gateway);
srv.startService();
}
if (gateway != null) {
LOG.info("Sent Modem Information:");
LOG.info("Sent Manufacturer: " + gateway.getManufacturer());
LOG.info("Sent Model: " + gateway.getModel());
LOG.info("Sent Serial No: " + gateway.getSerialNo());
LOG.info("Sent SIM IMSI: " + gateway.getImsi());
LOG.info("Sent Signal Level: " + gateway.getSignalLevel() + "%");
LOG.info("Sent Battery Level: " + gateway.getBatteryLevel() + "%");
}
// Send a message synchronously.
msg = new OutboundMessage(phone, content);
msg.setEncoding(MessageEncodings.ENCUCS2);// 這句話(huà)是發(fā)中文短信必須的
srv.sendMessage(msg);
}
/**
* 發(fā)送消息類(lèi)
* @author terry
*
*/
public class OutboundNotification implements IOutboundMessageNotification {
public void process(String gatewayId, OutboundMessage msg) {
LOG.info("Sent Outbound handler called from Gateway: " + gatewayId);
LOG.info(msg);
}
}
//接收消息類(lèi)
public String readMessage()
{
StringBuffer sb = new StringBuffer("");
List<InboundMessage> msgList;
// Create the notification callback method for Inbound & Status Report
// messages.
try
{
System.out.println("Read Example: Read messages from a serial gsm modem.");
System.out.println(Library.getLibraryDescription());
System.out.println("Read Version: " + Library.getLibraryVersion());
// Create new Service object - the parent of all and the main interface
// to you.
if (srv == null) {
srv = new Service();
srv.S.SERIAL_POLLING = true;
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
gateway.setInboundNotification(inboundNotification);
gateway.setCallNotification(callNotification);
srv.addGateway(gateway);
srv.startService();
}
// Similarly, you may define as many Gateway objects, representing
// various GSM modems, add them in the Service object and control all of them.
//
// Start! (i.e. connect to all defined Gateways)
LOG.info("Read Modem Information:");
LOG.info("Read Manufacturer: " + gateway.getManufacturer());
LOG.info("Read Model: " + gateway.getModel());
LOG.info("Read Serial No: " + gateway.getSerialNo());
LOG.info("Read SIM IMSI: " + gateway.getImsi());
LOG.info("Read Signal Level: " + gateway.getSignalLevel() + "%");
LOG.info("Read Battery Level: " + gateway.getBatteryLevel() + "%");
// 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);
int num = 1;
for (InboundMessage msg : msgList)
{
sb.append("第" + num + "條;發(fā)件人:"+msg.getOriginator() + ";內(nèi)容:" + msg.getText() + "\n");
//sb.append(msg.toString() + "\n");
LOG.info("第" + num + "條;發(fā)件人:"+msg.getOriginator() + ";內(nèi)容:" + msg.getText() + "\n");
num++;
LOG.info(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)
{
sb.append(e.getMessage());
e.printStackTrace();
}
finally
{
//this.srv.stopService();
}
return sb.toString();
}
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.
// 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);
}
}
}
相關(guān)文章
jxl操作excel寫(xiě)入數(shù)據(jù)不覆蓋原有數(shù)據(jù)示例
網(wǎng)上很多例子,都是用Jxl讀或者寫(xiě)excel,本文實(shí)現(xiàn)的功能就是將數(shù)據(jù)源in.xls的第幾行第幾列數(shù)據(jù)寫(xiě)入到out.xls的第幾行第幾列,不覆蓋out.xls其他原有的數(shù)據(jù)。2014-03-03
Spring?Boot統(tǒng)一處理全局異常的實(shí)戰(zhàn)教程
最近在做項(xiàng)目時(shí)需要對(duì)異常進(jìn)行全局統(tǒng)一處理,所以下面這篇文章主要給大家介紹了關(guān)于Spring?Boot統(tǒng)一處理全局異常的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
SpringCloud Gateway動(dòng)態(tài)轉(zhuǎn)發(fā)后端服務(wù)實(shí)現(xiàn)過(guò)程講解
這篇文章主要介紹了SpringCloud Gateway動(dòng)態(tài)轉(zhuǎn)發(fā)后端服務(wù)實(shí)現(xiàn)過(guò)程,簡(jiǎn)單的路由轉(zhuǎn)發(fā)可以通過(guò)SpringCloudGateway的配置文件實(shí)現(xiàn),在一些業(yè)務(wù)場(chǎng)景種,會(huì)需要?jiǎng)討B(tài)替換路由配置中的后端服務(wù)地址,單純靠配置文件無(wú)法滿(mǎn)足這種需求2023-03-03
舉例講解Java設(shè)計(jì)模式中的對(duì)象池模式編程
這篇文章主要介紹了Java設(shè)計(jì)模式中的對(duì)象池模式編程示例分享,對(duì)象池模式經(jīng)常在多線(xiàn)程開(kāi)發(fā)時(shí)被用到,需要的朋友可以參考下2016-02-02
Java實(shí)現(xiàn)的圖像查看器完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的圖像查看器,以完整實(shí)例形式較為詳細(xì)的分析了java處理圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
SpringMVC實(shí)現(xiàn)全局異常處理器的經(jīng)典案例
文章介紹了如何使用@ControllerAdvice和相關(guān)注解實(shí)現(xiàn)SpringMVC的全局異常處理,通過(guò)統(tǒng)一的異常處理類(lèi)和自定義業(yè)務(wù)異常類(lèi),可以將所有控制器的異常集中處理,并以JSON格式返回給前端,感興趣的朋友一起看看吧2025-03-03
IDEA創(chuàng)建springboot + mybatis項(xiàng)目全過(guò)程(步驟詳解)
這篇文章主要介紹了IDEA創(chuàng)建springboot + mybatis項(xiàng)目全過(guò)程及步驟詳解,本文通圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
HttpClient的RedirectStrategy重定向處理核心機(jī)制
這篇文章主要為大家介紹了HttpClient的RedirectStrategy重定向處理核心機(jī)制源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

