我正在尝试使用java发送短信,为此我首先尝试使用thilio.But我收到了底部提到的错误。然后我下载了,在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);
答案 0 :(得分:1)
为什么要尝试使用库?您通常可以通过电子邮件发送短信,只要您知道承运人。通常是number@carrier.com或类似的东西。
以下是包含运营商地址列表的网站:http://20somethingfinance.com/how-to-send-text-messages-sms-via-email-for-free/
答案 1 :(得分:0)
看起来你正在使用的Nexmo lib有一个你没有复制过的外部依赖。
来自Nexmo library documentation page:
<强>安装强>
您需要将nexmo-sdk.jar复制到您的应用程序并确保 它在你的类路径中。此外,还有一些图书馆 / lib目录下的依赖项也必须复制到您的 应用程序类路径,如果它们不存在。
因此,请查看您下载的SDK zipfile的/lib
目录,并将这些文件复制到您放置nexmo-sdk.jar的位置