?? sendmessage.java
字號:
package com.lxoa.common.sms;
import java.io.IOException;
import org.smslib.GatewayException;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.SMSLibException;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;
public class SendMessage
{
private static Service srv;
public SendMessage() {
}
public boolean inintMsgService(){
boolean result=false;
OutboundNotification outboundNotification = new OutboundNotification();
srv=new Service();
SerialModemGateway gateway = new SerialModemGateway("modem.com2", "COM2", 9600, "wavecom", null);
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
srv.setOutboundNotification(outboundNotification);
srv.addGateway(gateway);
try{
srv.startService();
result=true;
System.out.println("短信服務啟動成功!");
}catch(InterruptedException err1){
System.out.print(err1.getMessage()+"1");
}catch(SMSLibException err2){
System.out.print(err2.getMessage()+"2");
}catch(IOException err3){
System.out.print(err3.getMessage()+"3");
}
return result;
}
public boolean sendMessage(String content,String mobiles) throws Exception
{
String output="";
String[] phones=mobiles.split(";");
OutboundMessage msg;
boolean result=false;
for(int i=0;i<phones.length;i++){
msg=new OutboundMessage(phones[i],content);
msg.setEncoding(MessageEncodings.ENCUCS2);
try{
result=srv.sendMessage(msg);
System.out.print("\n["+"<"+phones[i]+">"+"發送成功 "+"]\n");
// output=output+"\n["+"<"+phones[i]+">"+"發送成功 "+"]";
}catch(InterruptedException err1){
System.out.print(err1.getMessage());
//output=output+"["+"<"+phones[i]+">"+err1.getMessage()+" "+"]";
}catch(TimeoutException err2){
System.out.print(err2.getMessage());
// output=output+"["+"<"+phones[i]+">"+err2.getMessage()+" "+"]";
}catch(IOException err3){
System.out.print(err3.getMessage());
// output=output+"["+"<"+phones[i]+">"+err3.getMessage()+" "+"]";
}catch(GatewayException err4){
System.out.print(err4.getMessage());
// output=output+"["+"<"+phones[i]+">"+err4.getMessage()+" "+"]";
}
}
return result;
}
public boolean colseMsgService()
{
boolean result=false;
try{
srv.stopService();
result=true;
}catch(InterruptedException err1){
System.out.print(err1.getMessage());
result=false;
}catch(SMSLibException err2){
System.out.print(err2.getMessage());
result=false;
}catch(IOException err3){
System.out.print(err3.getMessage());
result=false;
}
return result;
}
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);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -