我想从Web应用程序向手机发送短信,这有可能吗?我该怎么做?如何使用Java发送短信
回答
你可以使用这个免费的Java示例程序从您的PC使用连接到您的计算机的GSM调制解调器发送短信到您的COM端口。您还需要从Sun下载并安装Java通信API。
该程序需要下列java文件才能正常工作。
SerialConnection.java(此文件是用来从你的java程序连接到您的COM端口)
SerialConnectionException.java(此文件是在处理Java程序的串行连接的例外)
SerialParameters.java(此程序用于连接到从Java程序COM端口设置COM端口属性)
Sender.java(这是PROG RAM实现可运行并使用串行连接发送SMS)
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;
}
}
感谢拉蒂,我建立这个净豆,我可以知道如何发送短信可以通过电话联系我涉及的步骤 – Aravindkumar 2010-02-10 07:02:11
从谷歌复制一些东西似乎并不是一个很好的答案。 – 2010-02-10 07:47:48
专注于解决方案不打扰源mr.valentin – ratty 2010-02-10 07:52:15
最简单的方法是使用SMS网关。
这里有很多,我使用的是Clickatel,我只需发布一个XML请求,网关就可以完成剩下的任务。
我已经这样做了使用Java和apache commons HTTP Client
Here你可以找到在源锻造一个Java API的短信项目。
除此之外,您需要一个Sms Gateway的基础设施。一些公司为您提供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代码。
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 = "m[email protected]";
String to = "[email protected]";
String body = "Hello SMS World!";
Transport myTransport = null;
步骤-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());
步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或什么的。如果您发送电子邮件给; [email protected]它会通过短信将您的电子邮件发送到您的手机。
请查看SMSLib(http://smslib.org),这是一个使用GMS调制解调器或移动电话发送和接收短信的开源库。这真是一个伟大的图书馆。
我写了一个小行家lib中访问免费(为客户仅)网络瑞士手机类运营商的日出和橙色的界面。你可以在http://github.com/resmo/libjsms
上找到源码检索所有的手机Email-to-SMS(SMS Gateway)地址并发送一封电子邮件到该邮件地址。
- 1. Twilio短信发送错误,使用java
- 2. 如何使用sendTextMessage发送短信
- 3. 在java中发送短信
- 4. 如何使用短信管理器将短信发送到android
- 5. 如何连接oracle apex使用短信网关发送短信?
- 6. 使用GPRS发送短信
- 7. 使用Ruby发送短信
- 8. 使用API发送短信
- 9. 发送短信使用Python
- 10. 如何发送短信
- 11. 如何使用java向手机发送短信警报?
- 12. 如何发送短信到使用JAVA的手机
- 13. 如何不发送短信保存发送邮件后发送短信从SmsManager
- 14. 发送短信
- 15. 发送短信
- 16. 发送短信
- 17. 发送短信
- 18. 发送短信
- 19. 用于发送短信的短信不
- 20. 用java发送免费短信
- 21. 如何用Java发送短信,我关心的是regaridng短信网关
- 22. android如何发送短信和彩信?
- 23. 发送短信iPhone
- 24. Android发送短信
- 25. 短信发送ContentObserver
- 26. 短信未发送
- 27. 发送短信2008
- 28. sendTextMessage发送短信
- 29. 用数据发送短信
- 30. 用SmsManager发送短信
你也搜索了解。这里有很多关于发送短信的问题。 – Shoban 2010-02-10 05:26:19
@Shoban我认为表达您关心的最好方式是网站为您提供的机制,例如downvoting。当然,评论是其中一种方法,但无论问题的质量如何,粗鲁和讽刺的回应都不会被要求,只会拖拉网站。 – Howiecamp 2010-02-15 04:21:01
请参阅http://webmasters.stackexchange.com/questions/533/how-to-add-sms-text-messaging-functionality-to-my-website/1398#1398 – Adam 2010-11-16 01:32:38