java modem 短信发送

前一段时间公司用到短信modem发送信息,我通过参考网络资料,写了一个完整的例子供大家一块学习。代码
如下:

package cn.thunderbird.lm.media.util; 
import java.io.*; 
import java.util.Enumeration; 

import org.smslib.helper.CommPortIdentifier; 
import org.smslib.helper.SerialPort; 
public class ATCommandUtil { 
private String ATTEST = "AT\r"; 
private String ATSMSSET = "AT+CMGF=1\r"; 
private int COMMANDDELAYTIME = 500; //命令间隔延迟时间ms 
private int SMSTIME = 5000; //发送短信延迟时间ms 
boolean rs; 
public boolean sendMsg(String phoneNumber,String content,String portNO,int brandate){ 
String ATMSG ="AT+CMGS=\""+phoneNumber+"\"\r"; 
CommPortIdentifier portid=CommPortIdentifier.getPortIdentifier(portNO); 
Enumeration portList = CommPortIdentifier.getPortIdentifiers(); 
SerialPort com = null; 
if(portid.getPortType()==CommPortIdentifier.PORT_SERIAL){//判断是否为串口 
if(portid.getName().equals(portNO)){//获取指定串口号 
try{ 
com = portid.open("ATCommand", 2000); 
println("COM OK!"); 
int c; 
String response = ""; 
com.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN); 
com.setSerialPortParams(brandate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);//串口设置 
InputStream in = com.getInputStream(); 
OutputStream out = com.getOutputStream(); 
com.enableReceiveTimeout(1000); 
out.write(ATTEST.getBytes());//测试AT指令 
Thread.sleep(COMMANDDELAYTIME); 
c=in.read(); 
while(c!=-1){ 
if(c!='\r'){ 
response += (char)c; 
} 
c=in.read(); 
} 
if(response.indexOf("OK")>=0){//返回OK,代表AT指令正常 
print(new String(response.getBytes("iso-8859-1"),"GBK")); 
println("AT指令正常!\n"); 
} else{ 
println("AT指令出现异常,请检查端口号和波特率是否匹配!"); 
return false; 
} 
out.write(ATSMSSET.getBytes());//设置短信发送模式 
Thread.sleep(COMMANDDELAYTIME); 
response = ""; 
c=in.read(); 
while(c!=-1){ 
if(c!='\r'){ 
response += (char)c; 
} 
c=in.read(); 
} 

if(response.indexOf("OK")>=0){ 
print(response); 
println("短信设置TEXT模式成功!\n"); 
} else{ 
println("短信设置TEXT模式失败!"); 
return false; 
} 
out.write(ATMSG.getBytes());//发送指令AT+CMGS 
Thread.sleep(COMMANDDELAYTIME); 
response = ""; 
c=in.read(); 
while(c!=-1){ 
if(c!='\r'){ 
response += (char)c; 
} 
c=in.read(); 
} 
if(response.indexOf(">")>=0){ 
println(response); 
println("准备发送短信:"); 
println("短信内容:"+content); 
}else{ 
println("发送短信失败!"); 
return false; 
} 
out.write(content.getBytes());//发送短信内容 
println("发送短信..."); 
Thread.sleep(100); 
out.write(26);//发送CTRL+Z 
Thread.sleep(SMSTIME); 
response = ""; 
c=in.read(); 
while(c!=-1){ 
if(c!='\r'){ 
response += (char)c; 
} 
c=in.read(); 
} 
if(response.indexOf("OK")>=0){ 
println("短信发送成功!"); 
}else{ 
println("发送短信失败!"); 
return false; 
} 
}catch(Exception e){ 
println("端口号或者波特率不一致!"); 
println(e.getCause().toString()); 
return false; 
}finally{ 
if(com!=null){ 
com.close();//关闭端口 
println("COM is closed!"); 
} 
} 
}else{ 
return false; 
} 
}else{ 
return false; 
} 
return true; 
} 
public static void print(String msg){ 
System.out.print(msg); 
} 
public static void println(String msg){ 
System.out.println(msg); 
} 

 配置信息如下:

comm.jarwin32com.dll文件复制到JDK下面的jre/lib/ext.中, javax.comm.properties文件复制到JDK下面的jre/lib中。(文件都在短信modem所需文件配置.rar

资源下载:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值