【问题标题】:Unable to send SMS using java无法使用java发送短信
【发布时间】:2018-10-26 12:18:18
【问题描述】:

我只是尝试使用 java 发送 SMS,因为我在我的 Web 应用程序中需要它。但出于测试目的,我是此 site 中描述的代码 代码如下

package logic;

import com.harshadura.gsm.smsdura.GsmModem;

/**
 * @author     : Harsha Siriwardena  <harshadura@gmail.com>
 * @copyrights : www.Durapix.org     <http://www.durapix.org>
 * @license    : GNU GPL v3          <http://www.gnu.org/licenses/>
 *
 * Example on how to simply send a SMS using the smsdura API Wrapper.
 */
public class TestSMS {

    private static String port = "COM3"; //Modem Port.
    private static int bitRate = 115200; //this is also optional. leave as it is.
    private static String modemName = "ZTE"; //this is optional.
    private static String modemPin = "0000"; //Pin code if any have assigned to the modem.
    private static String SMSC = "+9477000003"; //Message Center Number ex. Mobitel

    public static void main(String[] args) throws Exception {
        GsmModem gsmModem = new GsmModem();
        GsmModem.configModem(port, bitRate, modemName, modemPin, SMSC);
        gsmModem.Sender("+94712244555", "Test Message"); // (tp, msg)
    }
}

当我尝试运行它时,我得到了这个错误

-----------------------------
*** SMS-DURA - GSM MODEM SMS API WRAPPER ***
www.harshadura.com
-----------------------------
Example: Send message from a serial gsm modem.
SMSLib: A Java API library for sending and receiving SMS via a GSM modem or other supported gateways.
This software is distributed under the terms of the Apache v2.0 License.
Web Site: http://smslib.org
Version: 3.5.1
log4j:WARN No appenders could be found for logger (smslib).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: javax.comm.NoSuchPortException
    at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
    at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
    at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
    at org.smslib.Service$1Starter.run(Service.java:276)

请任何人告诉我如何解决此问题

【问题讨论】:

  • 你是作为 web 应用还是独立应用运行的?
  • 你的调制解调器是正确的,根据这个配置?
  • Java comm 库现在有点过时了。使用 RxTx
  • 独立运行,我的意思是在eclipse中复制这段代码并运行它
  • 您确定调制解调器已连接到COM3 端口吗?例外似乎另有说明javax.comm.NoSuchPortException

标签: java sms smslib


【解决方案1】:

基本上你的程序找不到端口,使用

org.smslib.helper.CommPortIdentifier

从端口列表中找到正确的 COM 端口。

【讨论】:

  • 感谢您尝试帮助+1,但它不起作用,我遇到同样的错误
【解决方案2】:

我知道这不是您对 SO 期望的那种答案,但考虑到替代方案,我认为总比没有好。

a) 您的图书馆报告您的调制解调器未连接。
b) 您不知道如何检查您的调制解调器是否已连接。

虽然与我过去使用 nexmo 并取得巨大成功的公司没有任何关系。

如果这是您的应用程序的必需品我强烈建议您通过 API 途径解决问题,这将为您节省大量工作。

有几家公司提供此服务,并且使用或多或少是直接的,您只需格式化 URL 以将参数传递回公司。 Nexmo 示例:

// by calling a crafted url you are requesting the company to send the sms for you 
urlString = "https://rest.nexmo.com/sms/json?api_key={api_key}&api_secret={api_secret}&from=MyCompany20&to=447525856424&text=helloworld";
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream()

Nexmo 并不是唯一提供此服务的公司。他们是我唯一亲身体验过的人。

【讨论】:

  • 感谢您的帮助,所以 +1 但我的主要目标是在网络应用程序中使用。它(您提供的链接)是否会在网络应用程序中提供帮助
  • 我可以使用 wifi 热点代替 USB 调制解调器发送短信
  • @javaBeginner 我建议您将短信发送给外部提供商。您唯一需要的就是让您的网络应用程序能够访问网络。
  • @javaBeginner 应该是复制粘贴的问题。你有什么问题?
  • 与我在问题中发布的相同错误,请给我一个完整的运行代码来测试
【解决方案3】:

把API(comm.jar),dll(win32com),properties文件(javax.comm)复制进去

1) 如果您正在使用 Net Beans 工具意味着

C:\Program Files\Java\jdk1.7.0\jre\lib\ext

C:\Program Files\Java\jdk1.7.0\jre\bin

2) 如果您使用的是 Eclipse 工具,则意味着

C:\Program Files\Java\jre7\lib\ext

C:\Program Files\Java\jre7\bin

按照您下载此项目的 Code projects.com 中给出的说明。

另一种方式如果您可以配置您的工具然后执行此操作,那么逻辑是 API 应该在您的运行时 JVM(jre) 下。 并确保您提供的端口应该是免费的,其他应用程序不应在其上运行。

【讨论】:

    【解决方案4】:

    是的,我在 Harshadura 的 SMS 包装器中也遇到了这个例外。它们可能是你没有做的两件事

    • 您没有在代码中声明任何附加程序,所以在开头 您的代码只需输入这个简单的代码来声明附加程序。

      package logic;
      
      import com.harshadura.gsm.smsdura.GsmModem;
      
      /**
       * @author     : Harsha Siriwardena  <harshadura@gmail.com>
       * @copyrights : www.Durapix.org     <http://www.durapix.org>
       * @license    : GNU GPL v3          <http://www.gnu.org/licenses/>
       *
       * Example on how to simply send a SMS using the smsdura API Wrapper.
       */
      public class TestSMS {
      
      private static String port = "COM3"; //Modem Port.
      private static int bitRate = 115200; //this is also optional. leave as it is.
      private static String modemName = "ZTE"; //this is optional.
      private static String modemPin = "0000"; //Pin code if any have assigned to the modem.
      private static String SMSC = "+9477000003"; //Message Center Number ex. Mobitel
      
      public static void main(String[] args) throws Exception {
          BasicConfigurator.configure();//Declares appenders
          GsmModem gsmModem = new GsmModem();
          GsmModem.configModem(port, bitRate, modemName, modemPin, SMSC);
          gsmModem.Sender("+94712244555", "Test Message"); // (tp, msg)
      }    }
      
    • 识别 GSM 调制解调器的端口。您可以通过转到“我的电脑”图标单击管理 > 单击设备管理器> 单击调制解调器并识别调制解调器的端口来解决此问题。

    你识别的端口然后写在你的代码上。String port="MyPort";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 2020-05-01
      相关资源
      最近更新 更多