⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendmessage.java

📁 java使用simlib通过串口短信猫发送手机短信。simlib包和需要的文件都在里面
💻 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 + -