smslib发送短信

本文介绍如何使用Java实现发送短信的功能,并提供了所使用的jar包。包括短信发送的基本流程和示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在java中实现发送短信功能的API,附件是所用到的jar包。


import java.util.ArrayList ;
import java.util.List ;

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

import com.zhenjw.base.config.ReadeSystemConfig ;
import com.zhenjw.base.config.util ;

/**
* @author zhenjw
*
*/
public class SMSSend {

private ReadeSystemConfig readeSystemConfig=new ReadeSystemConfig();

private int defautlBaudReate= 9600;

private String comPort=readeSystemConfig.getsystem("smslib_comport");

public int send(String sendMobileNum,String sendContent) throws Exception
{
if(util.isNull(comPort))comPort="COM1";

return this.send("sms", sendMobileNum, sendContent, comPort, defautlBaudReate);
}

public int send(String id,String sendMobileNum,String sendContent, String comPort, int baudRate) throws Exception
{
return this.send(id, new String[]{sendMobileNum}, sendContent, comPort, baudRate);
}

public int send(String id,String sendMobileNum[],String sendContent, String comPort, int baudRate) throws Exception
{
OutboundNotification outboundNotification = new OutboundNotification();

//SerialModemGateway gateway = new SerialModemGateway("sms", "COM1", 9600, "", "");

if(baudRate<1)baudRate=defautlBaudReate;

//gateway.setSimPin("0000");
//gateway.setSmscNumber("+8613800100500");

Service service=Service.getInstance();

if(service.getGateway(id)==null)
{
SerialModemGateway gateway = new SerialModemGateway(id, comPort, baudRate, "", "");

gateway.setInbound(true);
gateway.setOutbound(true);
service.addGateway(gateway);

}

service.setOutboundMessageNotification(outboundNotification);

if(service.getServiceStatus()!= ServiceStatus.STARTED||service.getServiceStatus()!= ServiceStatus.STARTING)
{
service.startService();
}

// Send a message synchronously.
List msgs=new ArrayList();

for(int i=0,j=sendMobileNum.length;i<j;i++)
{

OutboundMessage msg = new OutboundMessage(sendMobileNum[i], sendContent);

msg.setEncoding(MessageEncodings.ENCUCS2);

msgs.add(msg);

}

int counter=service.sendMessages(msgs);

return counter;

}

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[])
{
SMSSend sMSSend=new SMSSend();

try {

long begin=System.currentTimeMillis();

int result=sMSSend.send("121", new String[]{"15332788592"}, "你好啊, 1324657890~!@#$%^&*()_+-={}[];:'\\<>/,./ smsLib", "COM1", 9600);

long end=System.currentTimeMillis();

System.out.println(" send num is "+result);

System.out.println(" used is "+(end -begin));

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值