如何使用Java发送短信

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

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

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

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

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

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

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

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

下载发送SMS 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; } } 

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

那里有很多,我使用过的是Clickatel ,我只是发布了一个XML请求,网关完成其余的工作。

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

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

除此之外,您还需要一个用于基础设施的Sms网关 。 有些公司为您提供API,使其变得像制作程序一样简单。

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

第2步。

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

步骤3。

在Java类的开头输入以下Java库。 这些库包括所需的Java Mail和Communications 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.*; 

步骤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 = "mySMSUsername@MySMSHost.com"; String to = "PhoneNumberToText@sms.MySMSHost.com"; String body = "Hello SMS World!"; Transport myTransport = null; 

步骤5创建Java代码以创建新的通信会话,然后将其用于配置文本消息中包含的信息。 然后准备发送此信息。 在第4步中输入的代码末尾的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()); 

步骤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` 

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

防爆。 你有Telia / Comviq / Chello或诸如此类的东西。 如果您发送电子邮件给; yournumber@youroperator.com它会通过短信将您的电子邮件发送到您的手机。

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

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

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