使用短信猫发送短信java代码
短信猫简单配置:https://www.cnblogs.com/Big-Boss/p/9699880.html
发送短信:
package utils; import org.smslib.AGateway; import org.smslib.IOutboundMessageNotification; import org.smslib.Message.MessageEncodings; import org.smslib.OutboundMessage; import org.smslib.Service; import org.smslib.modem.SerialModemGateway; /** * 短信猫RXTX——发送短信 * @author 【】 * */ public class SendMessageUtil { public void doIt(String phone,String content) throws Exception { OutboundNotification outboundNotification = new OutboundNotification(); // ---------------创建串口设备,如果有多个,就创建多个-------------- // 1、连接网关的id // 2、com口名称,如COM1或/dev/ttyS1(根据实际情况修改) // 3、串口波特率,如9600(根据实际情况修改) // 4、开发商 // 5、型号 SerialModemGateway gateway = new SerialModemGateway("modem.com6", "COM6", 9600, "null", "null"); gateway.setInbound(true); //设置true,表示该网关可以接收短信,根据需求修改 gateway.setOutbound(true); //设置true,表示该网关可以发送短信,根据需求修改 gateway.setSimPin("1234"); //sim卡锁,一般默认为0000或1234 //发送短信成功后的回调函方法 Service.getInstance().setOutboundMessageNotification(outboundNotification); Service.getInstance().addGateway(gateway); //将网关添加到短信猫服务中 Service.getInstance().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() + " dBm"); System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%"); //参数1:手机号码 参数2:短信内容 OutboundMessage msg = new OutboundMessage(phone, content); msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的 Service.getInstance().sendMessage(msg); //执行发送短信 System.out.println(msg); System.out.println("Now Sleeping - Hit <enter> to terminate."); System.in.read(); // 关闭服务 Service.getInstance().stopService(); } /* *短信发送成功后,调用该接口。并将发送短信的网关和短信内容对象传给process接口 */ public class OutboundNotification implements IOutboundMessageNotification{ public void process(AGateway gateway, OutboundMessage msg){ System.out.println("Outbound handler called from Gateway: " + gateway.getGatewayId()); System.out.println(msg); } } public static void main(String args[]) { SendMessageUtil app = new SendMessageUtil(); try { app.doIt("要发送的手机号", "要发送的短信内容"); } catch (Exception e) { e.printStackTrace(); } } }
发送成功的短信信息:
测试读取短信代码:https://www.cnblogs.com/Big-Boss/p/9700028.html