2010-02-10 122 views
7

我想从Web应用程序向手机发送短信,这有可能吗?我该怎么做?如何使用Java发送短信

+1

你也搜索了解。这里有很多关于发送短信的问题。 – Shoban 2010-02-10 05:26:19

+1

@Shoban我认为表达您关心的最好方式是网站为您提供的机制,例如downvoting。当然,评论是其中一种方法,但无论问题的质量如何,粗鲁和讽刺的回应都不会被要求,只会拖拉网站。 – Howiecamp 2010-02-15 04:21:01

+0

请参阅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

回答

0

你可以使用这个免费的Java示例程序从您的PC使用连接到您的计算机的GSM调制解调器发送短信到您的COM端口。您还需要从Sun下载并安装Java通信API。

该程序需要下列java文件才能正常工作。

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

  2. SerialConnectionException.java(此文件是在处理Java程序的串行连接的例外)

  3. SerialParameters.java(此程序用于连接到从Java程序COM端口设置COM端口属性)

  4. Sender.java(这是PROG RAM实现可运行并使用串行连接发送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; 


    } 
} 
+0

感谢拉蒂,我建立这个净豆,我可以知道如何发送短信可以通过电话联系我涉及的步骤 – Aravindkumar 2010-02-10 07:02:11

+3

从谷歌复制一些东西似乎并不是一个很好的答案。 – 2010-02-10 07:47:48

+4

专注于解决方案不打扰源mr.valentin – ratty 2010-02-10 07:52:15

2

Here你可以找到在源锻造一个Java API的短信项目。

除此之外,您需要一个Sms Gateway的基础设施。一些公司为您提供API,使其成为制作该程序一样容易。

2

第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 地点,以实例的J​​ava类和默认短信赋值库导入语句后,下面的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` 
+0

可能应该在代码的每一行之前添加四个空格;那么它会突出显示,而不是全部都在一条线上。 – icktoofay 2010-02-10 06:04:51

+2

也选择你的代码并按下ctrl + K也是如此。 – icktoofay 2010-02-10 06:05:25

+0

谢谢你icktoofay – ratty 2010-02-10 06:39:29

0

要做到这一点,最简单的办法是找到它通过邮件支持短信的运营商..

例。你有Telia/Comviq/Chello或什么的。如果您发送电子邮件给; [email protected]它会通过短信将您的电子邮件发送到您的手机。

0

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

0

上找到源码检索所有的手机Email-to-SMS(SMS Gateway)地址并发送一封电子邮件到该邮件地址。