天天看點

java使用comm口發送短信

1.從SMSLib網站下載下傳SMSLib代碼。下載下傳時,確定你下載下傳的是SMSLib-Java-v1.0.1.zip。SMSLib for Java可以跟Java通信API或RxTx一起使用 

2.以Java通信API 2.0作為開始,首先,確定你已經正确安裝了API。解壓javacomm20-win32.zip。在commapi子目錄中,你将找到如下檔案: 

javax.comm.properties 

win32com.dll 

comm.jar 

-------------------------- 

3.安裝java通信API 

把javax.comm.properties拷貝到你的Java運作時環境的lib目錄中(JDk和JRE都拷貝)。把win32com.dll拷貝到你的JRE的bin目錄中。 

設定PATH=.;c:\j2sdk1.4.2_03\jre\bin;。現在,使用如下指令來運作Java 黑盒程式: 

java -classpath .;../../comm.jar;BlackBox.jar; BlackBox 

如果Java通信API被正确的安裝了,那麼如圖所示,會出現一個顯示你機器的可用序列槽(COM端口)的Swing視窗

發送短信子產品:

import  java.util.ArrayList;   
import  java.util.List;   
import  java.util.regex.Matcher;   
import  java.util.regex.Pattern;   
  
import  org .smslib .IOutboundMessageNotification;   
import  org .smslib .OutboundMessage;   
import  org .smslib .Service ;   
import  org .smslib .Message.MessageEncodings;   
import  org .smslib .modem.SerialModemGateway;   
  
/**  
 * 短信發送測試類  
 * @author mazq  
 *  
 */   
public   class  SMSUtil{   
  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);   
    }   
 }   
  public   void  sendSMS(String mobilePhones,String content){   
//  System.out.println(mobilePhones+"--"+content);   
     Service  srv;   
     OutboundMessage msg;   
     OutboundNotification outboundNotification =  new  OutboundNotification();   
     srv =  new  Service ();   
//     SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 115200, "wavecom", "9600");   
     SerialModemGateway gateway =  new  SerialModemGateway( "modem.com1" ,  "COM1" ,  115200 ,  "wavecom" ,  "9600" );   
     gateway.setInbound( true );   
     gateway.setOutbound( true );   
     gateway.setSimPin( "0000" );   
     gateway.setOutboundNotification(outboundNotification);   
     srv.addGateway(gateway);   
     System.out.println( "初始化成功,準備開啟服務" );   
      try {   
      srv.startService();   
   System.out.println( "服務啟動成功" );   
    String[] phones = mobilePhones.split( "," );   
      for ( int  i= 0 ;i<phones.length;i++){   
      msg =  new  OutboundMessage(phones[i], content); //手機号碼,和短信内容   
      msg.setEncoding(MessageEncodings.ENCUCS2); //這句話是發中文短信必須的   
      srv.sendMessage(msg);   
      System.out.println(phones[i]+ " == " +content);   
     }   
     srv.stopService();   
    } catch (Exception e){   
     e.printStackTrace();   
    }   
 }   
  public   static   void  main(String[] args) {   
  SMSUtil util =  new  SMSUtil();   
  util.sendSMS( "1355xxxxxxx" , "測試短信" );   
 }   
  
}