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 {
/** This method will be called
 *  by SMSLib upon sending or when it failed to send a message.
*/ 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", "");
// com名称,串口号,破特率,连接设备名称,设备型号,sms pin
gateway.setInbound(true);// 设置网关可以写入信息 
gateway.setOutbound(true);// 设置网关可以读取信息  
gateway.setSimPin("0000");// 设置SIM PIN
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);System.out.println(msg.toString());System.out.println(gateway.getGatewayId());
//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("+86136********", "hello!");
System.out.println("发送完毕!!");
}
}

解决方案 »

  1.   

    在程序中配置了两测试输出:System.out.println(msg.toString());System.out.println(gateway.getGatewayId());
    控制台输出为:
    初始化成功,准备开启服务
    服务启动成功
    ===============================================================================
    << OutboundMessage >>
    -------------------------------------------------------------------------------
     Gateway Id: 
     Encoding: 7-bit
     Date: Mon Aug 01 14:53:02 CST 2011
     SMSC Ref No: 
     Recipient: +8613680***** 
     Dispatch Date: null
     Message Status: UNSENT
     Validity Period (Hours): -1
     Status Report: false
     Source / Destination Ports: -1 / -1
     Flash SMS: false
     Text: hello!
     PDU data: E8329BFD0E01
    ===============================================================================modem.COM3
    0 [main] WARN org.smslib  - GTW: modem.COM3: CMS Error 28: Retrying...