如何使用Java发送SMS

Ara*_*mar 6 java sms web-applications

我想通过网络应用程序向手机发送短信,是否可能?我该怎么做?

pst*_*ton 9

最简单的方法是使用SMS网关.

那里有很多,我使用的是Clickatel,我只是发布一个XML请求,网关完成剩下的工作.

我使用java和apache commons HTTP Client完成了这个


rat*_*tty 0

您可以使用这个免费的 Java 示例程序,使用连接到计算机的 GSM 调制解调器将 SMS 从 PC 发送到 COM 端口。您还需要从 Sun 下载并安装 Java comm api。

该程序需要以下 java 文件才能运行。

  1. SerialConnection.java(此文件用于从 java 程序连接到 COM 端口)

  2. SerialConnectionException.java(该文件用于处理Java程序中的串行连接异常)

  3. SerialParameters.java(此程序用于设置 COM 端口属性,以便从 java 程序连接到您的 com 端口)

  4. Sender.java(这是实现可运行并使用串行连接发送短信的程序)

  5. SMSClient.java(这个java类是可以在您自己的java程序中实例化并调用以发送短信的主类。该程序将依次在内部使用所有上述四个文件来发送您的短信)。

下载发送短信Java示例程序文件

/*
 *
 * A free Java sample program 
 * A list of java programs to send SMS using your COM serial connection
 * and a GSM modem
 *
 * @author William Alexander
 * free for use as long as this comment is included 
 * in the program as it is
 * 
 * More Free Java programs available for download 
 * at http://www.java-samples.com
 *
 *
 * Note: to use this program you need to download all the 5 java files
 * mentioned on top
 *
 */
public class SMSClient implements Runnable{

  public final static int SYNCHRONOUS=0;
  public final static int ASYNCHRONOUS=1;
  private Thread myThread=null;

  private int mode=-1;
  private String recipient=null;
  private String message=null;

  public int status=-1;
  public long messageNo=-1;


  public SMSClient(int mode) {
      this.mode=mode;
    }

  public int sendMessage (String recipient, String message){
    this.recipient=recipient;
    this.message=message;
    //System.out.println("recipient: " + recipient + " message: " + message);
    myThread = new Thread(this);
    myThread.start();
//    run();
    return status;
    }
    public void run(){

    Sender aSender = new Sender(recipient,message);

    try{
      //send message
          aSender.send ();

         // System.out.println("sending ... ");

      //in SYNCHRONOUS mode wait for return : 0 for OK,
      //-2 for timeout, -1 for other errors
      if (mode==SYNCHRONOUS) {
          while (aSender.status == -1){
            myThread.sleep (1000);
          }
      }
      if (aSender.status == 0) messageNo=aSender.messageNo ;

    }catch (Exception e){

        e.printStackTrace();

    }

    this.status=aSender.status ;

    aSender=null;


  }
}
Run Code Online (Sandbox Code Playgroud)

  • 专注于解决方案,不要担心来源瓦伦丁先生 (4认同)
  • 从谷歌复制一些东西对我来说似乎并不是一个好的答案。 (3认同)