如何使用Java发送短信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2234613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:03:55  来源:igfitidea点击:

How to send SMS using Java

javaweb-applicationssms

提问by Aravindkumar

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

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

采纳答案by ratty

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 示例程序,通过连接到您的计算机的 GSM 调制解调器从您的 PC 向您的 COM 端口发送 SMS。您还需要从 Sun 下载并安装 Java comm api。

This program needs the following java files to function.

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

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

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

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

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

  5. 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).

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

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

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

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

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

Download Send SMS Java sample program files

下载发送短信 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;


  }
}

回答by pstanton

the easiest way to do this is to use an SMS gateway.

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

there are lots out there, the one i've used is Clickatelto which i simply post an XMLrequest and the gateway does the rest for next to nothing.

那里有很多,我用过的一个是Clickatel,我只是向它发布一个XML请求,网关几乎没有做其余的工作。

i have done this using java and apache commons HTTP Client

我已经使用 java 和apache commons HTTP Client做到了这一点

回答by Kubi

Hereyou can find a Java SMS API project in source forge.

在这里,您可以在 source forge 中找到一个 Java SMS API 项目。

Apart from that, you need a Sms Gatewayfor the infrastructure. Some companies provide you APIs that it is becoming as easy as pie to make the program.

除此之外,您还需要一个用于基础设施的短信网关。一些公司为您提供 API,使程序变得像馅饼一样容易。

回答by ratty

Step-1. Download Mail.jar and Activation.jar (see Resources for links) and save to the Java library directory on your computer's local drive.

第1步。下载 Mail.jar 和 Activation.jar(请参阅参考资料中的链接)并保存到计算机本地驱动器上的 Java 库目录中。

Step-2.

第2步。

Start a new Java class in your Java Integrated Development Environment (IDE) and name it "MyMobileJava.java".

在 Java 集成开发环境 (IDE) 中启动一个新的 Java 类并将其命名为“MyMobileJava.java”。

Step-3.

第 3 步。

Enter the following Java libraries at the start of your Java class. These libraries include the required Java Mail and Communications API resources and other supporting Input/Output and Internet class libraries for sending SMS text messages.

在 Java 类的开头输入以下 Java 库。这些库包括所需的 Java 邮件和通信 API 资源以及用于发送 SMS 文本消息的其他支持输入/输出和 Internet 类库。

import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

Step-4 Place the following Java code after the library import statements in order to instantiate the Java class and assign values for the default SMS text messages.

步骤 4 将以下 Java 代码放在库导入语句之后,以便实例化 Java 类并为默认 SMS 文本消息赋值。

public class SMTPSend {

        public SMTPSend() {
        }

        public void msgsend() {
          String username = "MySMSUsername";
          String password = "MyPassword";
          String smtphost = "MySMSHost.com";
          String compression = "My SMS Compression Information";
          String from = "[email protected]";
          String to = "[email protected]";
          String body = "Hello SMS World!";
          Transport myTransport = null;

Step-5 Create Java code to create a new communications session that will then be used to configure the information contained within a text message. This information will then be prepared to be sent. Enter the following Java code in your Java class at the end of the code entered in step four.

步骤 5 创建 Java 代码以创建一个新的通信会话,然后将用于配置文本消息中包含的信息。然后将准备发送此信息。在第四步中输入的代码末尾的 Java 类中输入以下 Java 代码。

 try {
    Properties props = System.getProperties();
    props.put("mail.smtp.auth", "true");
    Session mailSession = Session.getDefaultInstance(props, null);
    Message msg = new MimeMessage(mailSession);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address = {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(compression);
    msg.setText(body);
    msg.setSentDate(new Date());

Step-6 Send the text message by connecting to your SMS host address, saving changes to the message, and then sending the information. To do this, enter the following Java code to finish the Java class.

步骤 6 通过连接到您的 SMS 主机地址来发送文本消息,保存对消息的更改,然后发送信息。为此,请输入以下 Java 代码以完成 Java 类。

     myTransport = mailSession.getTransport("smtp");
      myTransport.connect(smtphost, username, password);
      msg.saveChanges();
      myTransport.sendMessage(msg, msg.getAllRecipients());
      myTransport.close();
     } catch (Exception e) {
        e.printStackTrace();
      }
   }

   public static void main(String[] argv) {
     SMTPSend smtpSend = new SMTPSend();
     smtpSend.msgsend();
   }
 } //enter code here`

回答by ratty

The easiest way to do this is to find an operator which supports sms's through mail..

最简单的方法是找到一个支持通过邮件发送短信的运营商。

Ex. You have Telia/Comviq/Chello or whatnot. If you send an email to; [email protected] it will send your email via sms to your phone.

前任。你有 Telia/Comviq/Chello 之类的。如果您发送电子邮件至;[email protected] 它将通过短信将您的电子邮件发送到您的手机。

回答by Elias Haileselassie

Please have a look at SMSLib (http://smslib.org), an open source library for sending and receiving sms using a GMS modem or a mobile phone. It is really a great library.

请查看 SMSLib ( http://smslib.org),这是一个使用 GMS 调制解调器或手机发送和接收短信的开源库。这真的是一个很棒的图书馆。

回答by resmo

I wrote a small maven lib for accessing the free (for customers only) web interface of the Swiss moblie operators Sunrise and Orange. You find the source on http://github.com/resmo/libjsms

我编写了一个小的 maven 库,用于访问瑞士移动运营商 Sunrise 和 Orange的免费(仅限客户)Web 界面。您可以在http://github.com/resmo/libjsms上找到源代码

回答by resmo

Just retrieve all the cell phone Email-to-SMS (SMS Gateway) addresses and send an email to that email-to-SMS address.

只需检索所有手机电子邮件到短信(SMS 网关)地址并向该电子邮件到短信地址发送电子邮件。