最简单的方法是使用短信网关。
有很多选择,我使用的是 Clickatel ,只需要发送一个 XML 请求,网关便会以极低的价格完成其余工作。
我使用了 java 和 apache commons HTTP Client 完成这个过程。
步骤一: 下载Mail.jar和Activation.jar(请参阅资源链接),并将其保存到计算机本地驱动器上的Java库目录中。
步骤二: 在Java集成开发环境(IDE)中启动一个新的Java类,并将其命名为"MyMobileJava.java"。
步骤三: 在您的Java类开头输入以下Java库。这些库包括所需的Java邮件和通信API资源,以及其他支持输入/输出和互联网类库,用于发送SMS文本消息。
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.*;
第四步 在库导入语句之后放置以下Java代码,以实例化Java类并为默认的短信文本消息分配值。
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;
第五步 创建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());
第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`
SerialConnection.java(此文件用于从Java程序连接到COM端口)
SerialConnectionException.java(此文件用于处理Java程序中的串行连接异常)
SerialParameters.java(此程序用于设置您的COM端口属性,以便从Java程序连接到com端口)
Sender.java(这是实现可运行性并使用串行连接发送SMS的程序)
SMSClient.java(此Java类是可以在您自己的Java程序中实例化并调用以发送SMS的主类。该程序将在内部使用上述四个文件来发送您的SMS)。
/*
*
* 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;
}
}
我编写了一个小的Maven库,用于访问瑞士移动运营商Sunrise和Orange的免费(仅限客户)Web界面。您可以在http://github.com/resmo/libjsms上找到源代码。