<code id="dxkde"><ol id="dxkde"></ol></code><code id="dxkde"><u id="dxkde"><span id="dxkde"></span></u></code>

  • <output id="dxkde"><legend id="dxkde"></legend></output>
    短信積分商城 400-6699-521 咨詢熱線: 0371-69067686
    行業動態Industry Dynamics
    您的位置: 首頁 > 樂信行業動態 >

    Java發送手機短信的方法及程序源代碼

    發布時間:2015-09-06
      Java發送手機短信需要什么呢?如何通過Java程序實現群發手機短信呢?下面樂信短信平臺小編通過以下方法實現java發送手機短信。用 Java 通過串口發短信其實很簡單,因為有現成的類庫供我們 使用。有底層的類庫,也有封裝好一點的類庫,下面我介紹一下在 Win32 平臺下發送短信的方法。
      如果你想用更底層的類庫開發功能更強大的應用程序有兩種選擇:一個是 SUN Java Comm(下載地址:javacomm20-win32.zip),另一個是 RxTx (下載地址:Rxtx.zip)
      用這兩個類庫開發程序的先決條件是你要很好掌握串口的知識,比如 AT 命令等,這些基本知識學起來是需要一定時間的,不過不用擔心,現在也有開源的已經封裝好的類庫供我們使用,這就是  SMSLib - SMS Processing Libraries (下載地址:SMSLib),今后的幾篇系列文章主要目的就是介紹 SMSLib 的使用,今天首先就是要安裝環境了,注意最新的 SMSLib 只能在 JRE5.0 或以后的版本才 能運行。
      SMSLib 也是構建在 SUN Java Comm 和 RxTx 基礎之上的,這兩個類庫你可以自己選擇,不過 SMSLib 默認采用的是 SUN Java Comm(即你下載下來的 SMSLib 包中的 dist\lib 目錄下的 smslib- 2.1.2.jar 是用 SUN Java Comm 編譯生成的),
      如果你想更換成 RxTx 就需要更改 SMSLib 的源文件并重新編譯,具體方法為:
      將 src\java\org\smslib 文件夾下的 CSerialDriver.java 這個文件的 "import javax.comm.*;" 修改為 "import gnu.io.*;",然后用 ant 工具重新編譯生成的 jar 文件就是基于 RxTx 實現的 。
      下面介紹一下 SUN Java Comm 和 RxTx 的安裝過程:
      ①. SUN Java Comm:
      將 "javax.comm.properties" 拷貝到 "%JREHOME%/lib" 目錄下
      將 "win32com.dll" 拷貝到 "%JREHOME%/bin" 目錄下
      ②. RxTx
      將 "rxtxSerial.dll" 拷貝到 "%JREHOME%/bin" 目錄下
      然后將 "comm.jar" 或者 "RXTXComm.jar" 加載到你的工程環境中就可以了,另外還要記著下載 log4j,因為 SMSLib 用到了它。
      如果對串口有興趣的朋友可以去研究一下 AT 命令,直接使用 SUN Java Comm 和 RxTx 編寫應用程序,可以實現更強大的功能。
      在開發手機短信開發之前,需要下載JAVA的串口訪問接口類包
      我先到SUN的網站 ,只找到了版本Java(tm) Communication API 3.0 Update 1 for Solaris x86  和 Solaris 8 & 9 -Sparc 和 Linux,沒有找到WINDOWS平臺的。后來在 http://www.matrix.org.cn/thread.shtml?topicId=37246&forumId=1找到了Java(tm) Communication API 2.0WINDOWS平臺的版本。在下載解壓將其添加到LIB中,然后就可以開始手機短信的開發了。
      java發送手機短信源代碼:
      ReadSerial類
      package com.nlie.pub_com.msgmgr;
      import java.io.*;
      /**
      *
      * This class reads message from the specific serial port and save
      * the message to the serial buffer.
      *
      */
      public class ReadSerial
      extends Thread {
      private SerialBuffer ComBuffer;
      private InputStream ComPort;
      /**
      *
      * Constructor
      *
      * @param SB The buffer to save the incoming messages.
      * @param Port The InputStream from the specific serial port.
      *
      */
      public ReadSerial(SerialBuffer SB, InputStream Port) {
      ComBuffer = SB;
      ComPort = Port;
      }
      public void run() {
      int c;
      try {
      while (true) {
      c = ComPort.read();
      ComBuffer.PutChar(c);
      }
      }
      catch (IOException e) {}
      }
      }
      SerialBean類
      package com.nlie.pub_com.msgmgr;
      import java.io.*;
      import java.util.*;
      import javax.comm.*;
      /**
      *
      * This bean provides some basic functions to implement full dulplex
      * information exchange through the srial port.
      *
      */
      public class SerialBean {
      static String PortName;
      CommPortIdentifier portId;
      SerialPort serialPort;
      static OutputStream out;
      static InputStream in;
      SerialBuffer SB;
      ReadSerial RT;
      /**
      *
      * Constructor
      *
      * @param PortID the ID of the serial to be used. 1 for COM1,
      * 2 for COM2, etc.
      *
      */
      public SerialBean(int PortID) {
      PortName = "COM" + PortID;
      }
      /**
      *
      * This function initialize the serial port for communication. It starts a
      * thread which consistently monitors the serial port. Any signal captured
      * from the serial port is stored into a buffer area.
      *
      */
      public int Initialize() {
      int InitSuccess = 1;
      int InitFail = -1;
      try {
      portId = CommPortIdentifier.getPortIdentifier(PortName);
      try {
      serialPort = (SerialPort)
      portId.open("Serial_Communication", 2000);
      }
      catch (PortInUseException e) {
      return InitFail;
      }
      //Use InputStream in to read from the serial port, and OutputStream
      //out to write to the serial port.
      try {
      in = serialPort.getInputStream();
      out = serialPort.getOutputStream();
      }
      catch (IOException e) {
      return InitFail;
      }
      //Initialize the communication parameters to 9600, 8, 1, none.
      try {
      serialPort.setSerialPortParams(9600,
      SerialPort.DATABITS_8,
      SerialPort.STOPBITS_1,
      SerialPort.PARITY_NONE);
      }
      catch (UnsupportedCommOperationException e) {
      return InitFail;
      }
      }
      catch (NoSuchPortException e) {
      return InitFail;
      }
      // when successfully open the serial port, create a new serial buffer,
      // then create a thread that consistently accepts incoming signals from
      // the serial port. Incoming signals are stored in the serial buffer.
      SB = new SerialBuffer();
      RT = new ReadSerial(SB, in);
      RT.start();
      // return success information
      return InitSuccess;
      }
      /**
      *
      * This function returns a string with a certain length from the incoming
      * messages.
      *
      * @param Length The length of the string to be returned.
      *
      */
      public String ReadPort(int Length) {
      String Msg;
      Msg = SB.GetMsg(Length);
      return Msg;
      }
      /**
      *
      * This function sends a message through the serial port.
      *
      * @param Msg The string to be sent.
      *
      */
      public void WritePort(String Msg) {
      int c;
      try {
      for (int i = 0; i < Msg.length(); i++) {
      out.write(Msg.charAt(i));
      }
      }
      catch (IOException e) {}
      }
      /**
      *
      * This function closes the serial port in use.
      *
      */
      public void ClosePort() {
      RT.stop();
      serialPort.close();
      }
      }
      SerialBuffer類
      package com.nlie.pub_com.msgmgr;
      public class SerialBuffer {
      private String Content = "";
      private String CurrentMsg, TempContent;
      private boolean available = false;
      private int LengthNeeded = 1;
      /**
      *
      * This function returns a string with a certain length from the incoming
      * messages.
      *
      * @param Length The length of the string to be returned.
      *
      */
      public synchronized String GetMsg(int Length) {
      LengthNeeded = Length;
      notifyAll();
      if (LengthNeeded > Content.length()) {
      available = false;
      while (available == false) {
      try {
      wait();
      }
      catch (InterruptedException e) {}
      }
      }
      CurrentMsg = Content.substring(0, LengthNeeded);
      TempContent = Content.substring(LengthNeeded);
      Content = TempContent;
      LengthNeeded = 1;
      notifyAll();
      return CurrentMsg;
      }
      /**
      *
      * This function stores a character captured from the serial port to the
      * buffer area.
      *
      * @param t The char value of the character to be stored.
      *
      */
      public synchronized void PutChar(int c) {
      Character d = new Character( (char) c);
      Content = Content.concat(d.toString());
      if (LengthNeeded < Content.length()) {
      available = true;
      }
      notifyAll();
      }
      }
      mobi_operate_bean類
      package com.nlie.pub_com.msgmgr;
      public class mobi_operate_bean
      extends Thread {
      private boolean be_send = false;
      private static char symbol = 10;
      private static char symbol1 = 13;
      private static SerialBean SB = null;
      /*
      private String message_cont = null;
      private String sms_send_no = null;
      public void setmessage_cont(String strsmscont) {
      message_cont = strsmscont;
      }
      public void setsms_send_no(String strsms_send_no) {
      sms_send_no = strsms_send_no;
      }
      public String getmessage_cont() {
      return message_cont;
      }
      public String getsms_send_no() {
      return sms_send_no;
      }
      */
      //************************************************************
      //函數名:delete_sms(int SmsNo,int DelFlag)
      //參數說明
      //smsNo:需要刪除的短消息序號
      //DelFlag:刪除標記 0 為當前序號 1 所有讀過的記錄 2 所有讀過和發送的記錄
      //        3 所有讀過和發送和未發送的記錄 4 所有短消息
      //作者:樂信
      //時間:2002年4月14日
      public static boolean delete_sms(int SmsNo, int DelFlag) {
      String strReturn, strSend;
      strReturn = "";
      strSend = "";
      try {
      strSend = "AT+CMGD=" + String.valueOf(DelFlag) + String.valueOf(symbol) +
      String.valueOf(symbol1);
      SB.WritePort(strSend);
      sleep(200);
      strReturn = SB.ReadPort(10);
      if (strReturn.indexOf("OK", 0) != -1) {
      return true;
      }
      return false;
      }
      catch (Exception ex) {
      ex.printStackTrace();
      return false;
      }
      }
      //************************************************************
      //函數名:opencomm(int commport)
      //參數說明
      //commport:需要打開的串口號
      //返回:
      //   false  打開串口失敗
      //   true   打開串口成功
      //作者:樂信
      //時間:2002年4月14日
      public static boolean opencomm(int commport) {
      SB = new SerialBean(commport);
      if (SB.Initialize() == 1) {
      return true;
      }
      else {
      return false;
      }
      }
      //************************************************************
      //函數名:testmobi
      //參數說明
      //
      //返回:
      //   false  測試失敗
      //   true   測試成功
      //作者:樂信
      //時間:2002年4月14日
      public static boolean testmobi() {
      String strReturn, strSend;
      strReturn = "";
      strSend = "";
      strSend = "AT" + String.valueOf(symbol1);
      try {
      SB.WritePort(strSend);
      sleep(200);
      strReturn = SB.ReadPort(9);
      if (strReturn.indexOf("OK", 0) != -1) {
      strSend = "ATE0" + String.valueOf(symbol1);
      SB.WritePort(strSend);
      strReturn = "";
      sleep(200);
      strReturn = SB.ReadPort(11);
      if (strReturn.indexOf("OK", 0) != -1) {
      return true;
      }
      }
      return false;
      }
      catch (Exception ex) {
      ex.printStackTrace();
      return false;
      }
      }
      //************************************************************
      //函數名:setmode(int mode)
      //參數說明
      //mode:短信發送模式
      //返回說明:
      //      false   設置模式失敗
      //      true    設置模式成功
      //作者:樂信
      //時間:2002年4月14日
      private static boolean setmode(int mode) {
      String strReturn, strSend;
      strSend = "";
      try {
      strSend = "AT+CMGF=" + String.valueOf(mode) +
      String.valueOf(symbol1);
      SB.WritePort(strSend);
      strReturn = "";
      sleep(200);
      strReturn = SB.ReadPort(6);
      if (strReturn.indexOf("OK", 0) != -1) {
      return true;
      }
      return false;
      }
      catch (Exception ex) {
      ex.printStackTrace();
      return false;
      }
      }
      //************************************************************
      //函數名:sendMessage
      //參數說明
      //   Receiver:短信接收者的手機號碼
      //   sms_content:短信內容
      //返回:
      //   false  發送失敗
      //   true   發送成功
      //作者:樂信
      //時間:2002年4月14日
      public static boolean sendMessage(String Receiver, String sms_content) {
      String strReturn, strSend;
      char symbol2 = 34;
      char symbol3 = 26;
      strReturn = "";
      strSend = "";
      if (setmode(1) != true) {
      return false;
      }
      try {
      strSend = "AT+CSMP=1,173,36,08" + String.valueOf(symbol1);
      System.out.println("step 1 begin");
      SB.WritePort(strSend);
      sleep(400);
      strReturn = SB.ReadPort(6);
      if (strReturn.indexOf("OK", 0) != -1) {
      System.out.println("step 1 ok");
      strSend = "AT+CMGS=" + String.valueOf(symbol2) + Receiver +
      String.valueOf(symbol2) +
      String.valueOf(symbol1);
      System.out.println("step 2 begin");
      SB.WritePort(strSend);
      strReturn = "";
      sleep(200);
      strReturn = SB.ReadPort(4);
      System.out.println("step 3 begin");
      byte[] str1 = null;
      try {
      str1 = sms_content.getBytes("GBK");
      }
      catch (java.io.UnsupportedEncodingException e) {
      e.printStackTrace();
      }
      strSend = encodeHex(str1, sms_content) + String.valueOf(symbol3) +
      String.valueOf(symbol1);
      strReturn = "";
      System.out.println("step 4 begin");
      SB.WritePort(strSend);
      sleep(2000);
      strReturn = "";
      strReturn = SB.ReadPort(8);
      if (strReturn.indexOf("+CMGS", 0) != -1) {
      System.out.println("OK");
      return true;
      }
      }
      return false;
      }
      catch (Exception ex) {
      ex.printStackTrace();
      return false;
      }
      }
      //************************************************************
      //函數名:getMessage
      //參數說明
      //   Way:短信接收者的手機號碼
      //   m_Mode:短信內容
      //返回:
      //   false  發送失敗
      //   true   發送成功
      //作者:樂信
      //時間:2002年1月5日
      public static String getMessage(int Way, int m_Mode) {
      String strReturn, strSend;
      strReturn = "";
      strSend = "";
      String strtemp = null;
      if (m_Mode == 0) {
      switch (Way) {
      case 0:
      strtemp = "REC UNREAD";
      break;
      case 1:
      strtemp = "REC READ";
      break;
      case 2:
      strtemp = "STO UNSENT";
      break;
      case 3:
      strtemp = "STO SENT";
      break;
      case 4:
      strtemp = "ALL";
      break;
      }
      }
      else {
      switch (Way) {
      case 0:
      strtemp = "REC UNREAD";
      break;
      case 1:
      strtemp = "REC READ";
      break;
      case 2:
      strtemp = "STO UNSENT";
      break;
      case 3:
      strtemp = "STO SENT";
      break;
      case 4:
      strtemp = "ALL";
      break;
      }
      }
      try {
      strSend = "AT+CMGL=" + strtemp + String.valueOf(symbol1);
      SB.WritePort(strSend);
      strReturn = SB.ReadPort(256);
      if (strReturn.indexOf("OK", 0) != -1) {
      if (strReturn.indexOf("+CMGL", 0) != -1) {
      int i = strReturn.indexOf(":");
      strtemp = strReturn.substring(i, strReturn.length());
      return strtemp;
      }
      }
      return null;
      }
      catch (Exception ex) {
      ex.printStackTrace();
      return null;
      }
      }
      public mobi_operate_bean() {
      }
      //************************************************************
      //函數名:encodeHex
      //參數說明
      //   bytes:短信內容BYTE值
      //   sms_content:短信內容
      //返回:
      //   返回解碼的PDU格式短信內容
      //
      //作者:樂信
      //時間:2002年1月5日
      private static final String encodeHex(byte[] bytes, String sms_content) {
      StringBuffer buff = new StringBuffer(bytes.length * 4);
      String b;
      char a;
      int n = 0;
      int m = 0;
      for (int i = 0; i < bytes.length; i++) {
      b = Integer.toHexString(bytes[i]);
      if (bytes[i] > 0) {
      buff.append("00");
      buff.append(b);
      n = n + 1;
      }
      else {
      a = sms_content.charAt( (i - n) / 2 + n);
      m = a;
      b = Integer.toHexString(m);
      buff.append(b.substring(0, 4));
      i = i + 1;
      }
      }
      return buff.toString();
      }
      public static void main(String[] args) {
      mobi_operate_bean.opencomm(3);
      mobi_operate_bean.testmobi();
      // String smscont = mobi_operate_bean.getMessage(1, 2);
      mobi_operate_bean.sendMessage("186********",
      "樂信短信平臺,高效優質的群發短信平臺,采用短信發送成功計費、失敗返款的模式,為企業提供企信通短信平臺、企業短信、網頁短信群發平臺等移動商務的行業方案整合服務。");
      }
      }

    (責任編輯:樂信短信平臺)
    七七电影网