java通信

Java代码
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;

/**
* 短信发送测试类
* @author mazq
*
*/
public class SMSUtil{
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 void sendSMS(String mobilePhones,String content){
// System.out.println(mobilePhones+"--"+content);
Service srv;
OutboundMessage msg;
OutboundNotification outboundNotification = new OutboundNotification();
srv = new Service();
// SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 115200, "wavecom", "9600");
SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 115200, "wavecom", "9600");
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);
System.out.println(phones[i]+" == "+content);
}
srv.stopService();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
SMSUtil util = new SMSUtil();
util.sendSMS("1355xxxxxxx","测试短信");
}

}
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;

/**
* 短信发送测试类
* @author mazq
*
*/
public class SMSUtil{
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 void sendSMS(String mobilePhones,String content){
// System.out.println(mobilePhones+"--"+content);
Service srv;
OutboundMessage msg;
OutboundNotification outboundNotification = new OutboundNotification();
srv = new Service();
// SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 115200, "wavecom", "9600");
SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 115200, "wavecom", "9600");
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);
System.out.println(phones[i]+" == "+content);
}
srv.stopService();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
SMSUtil util = new SMSUtil();
util.sendSMS("1355xxxxxxx","测试短信");
}

}

如果收到短信,则说明ok了

如果在日志中输出下面的内容,一般是说你的commons-net-1.4.1.jar或comm.jar没有配置到classpath中
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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值