smslib发送短信

在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();
		}
		
	}
}

你可能感兴趣的:(sms)