0

我试图使用java发送短信,为此我首先尝试使用thilio。但我得到了底部提到的错误。然后我下载,在nexmo中创建了一个帐户,但我再次收到同样的错误。请任何有过的人已经在上面两个工作过可以告诉我为什么会出现这个问题。

import com.nexmo.messaging.sdk.NexmoSmsClient;
import com.nexmo.messaging.sdk.SmsSubmissionResult;
import com.nexmo.messaging.sdk.messages.TextMessage;

public class SendTextMessage {

    public static final String API_KEY = "apikey";
    public static final String API_SECRET = "apisecret";

    public static final String SMS_FROM = "singapore_number";// as i use my office number so can not disclose it
    public static final String SMS_TO = "singapre_number";
    public static final String SMS_TEXT = "Hello World!";

    public static void main(String[] args) {

        // Create a client for submitting to Nexmo

        NexmoSmsClient client = null;
        try {
            client = new NexmoSmsClient(API_KEY, API_SECRET);
        } catch (Exception e) {
            System.err.println("Failed to instanciate a Nexmo Client");
            e.printStackTrace();
            throw new RuntimeException("Failed to instanciate a Nexmo Client");
        }

        // Create a Text SMS Message request object ...

        TextMessage message = new TextMessage(SMS_FROM, SMS_TO, SMS_TEXT);

        // Use the Nexmo client to submit the Text Message ...

        SmsSubmissionResult[] results = null;
        try {
            results = client.submitMessage(message);
        } catch (Exception e) {
            System.err.println("Failed to communicate with the Nexmo Client");
            e.printStackTrace();
            throw new RuntimeException("Failed to communicate with the Nexmo Client");
        }

        // Evaluate the results of the submission attempt ...
        System.out.println("... Message submitted in [ " + results.length + " ] parts");
        for (int i=0;i<results.length;i++) {
            System.out.println("--------- part [ " + (i + 1) + " ] ------------");
            System.out.println("Status [ " + results[i].getStatus() + " ] ...");
            if (results[i].getStatus() == SmsSubmissionResult.STATUS_OK)
                System.out.println("SUCCESS");
            else if (results[i].getTemporaryError())
                System.out.println("TEMPORARY FAILURE - PLEASE RETRY");
            else
                System.out.println("SUBMISSION FAILED!");
            System.out.println("Message-Id [ " + results[i].getMessageId() + " ] ...");
            System.out.println("Error-Text [ " + results[i].getErrorText() + " ] ...");

            if (results[i].getMessagePrice() != null)
                System.out.println("Message-Price [ " + results[i].getMessagePrice() + " ] ...");
            if (results[i].getRemainingBalance() != null)
                System.out.println("Remaining-Balance [ " + results[i].getRemainingBalance() + " ] ...");
        }
    }

}

错误如下

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
    at SendTextMessage.main(SendTextMessage.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 1 more

第 32 行表示这一行 client = new NexmoSmsClient(API_KEY, API_SECRET);

4

2 回答 2

1

您为什么要尝试使用库?只要您知道运营商,您通常可以通过电子邮件发送 SMS 文本。通常它是 number@carrier.com 或类似的东西。

这是一个包含运营商地址列表的网站:http: //20somethingfinance.com/how-to-send-text-messages-sms-via-email-for-free/

于 2013-10-19T11:48:31.990 回答
0

看起来您正在使用的 Nexmo 库有一个您没有复制的外部依赖项。

从 Nexmo库文档页面

安装

您需要将 nexmo-sdk.jar 复制到您的应用程序并确保它在您的类路径中。此外,/lib 目录下还有一些库依赖项,如果它们不存在,也必须将它们复制到您的应用程序类路径中。

因此,查看/lib您下载的 SDK zipfile 的目录,并将这些文件复制到您放置 nexmo-sdk.jar 的任何位置

于 2014-05-29T18:03:30.553 回答