如何使用Java发送短信 [英] How to send SMS using Java

查看:18
本文介绍了如何使用Java发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网络应用程序向手机发送短信,可以吗?我该怎么做?

I want to send SMS to a mobile phone from a web application, is it possible? How do I do it?

推荐答案

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

You can use this free Java sample program to send SMS from your PC using GSM modem connected to your computer to your COM port. You also need to download and install the Java comm api from Sun.

此程序需要以下 java 文件才能运行.

This program needs the following java files to function.

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

  1. SerialConnection.java (This file is used to connect to your COM port from your java program)

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

SerialConnectionException.java (This file is for handling serial connection exceptions in your Java program)

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

SerialParameters.java (This program is used to set your COM port properties for connecting to your com port from your java program)

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

Sender.java (This is the program that implements runnable and sends SMS using the serial connection)

SMSClient.java(这个java类是主类,可以在你自己的java程序中实例化并调用发送短信.这个程序内部会依次使用以上四个文件来发送你的短信).

SMSClient.java (This java class is the main class that can be instantiated in your own java program and called to send SMS. This program in turn will use all the above four files internally to send out your SMS).

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

Download Send SMS Java sample program files

/*
 *
 * 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;


  }
}

这篇关于如何使用Java发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆