2013-10-19 46 views
0

我試圖發送短信使用Java和爲此,我第一次嘗試與thilio.But我得到的錯誤,提到底部。然後我下載,在nexmo中創建一個帳戶,但又我得到了同樣的錯誤。請那些已經在上面兩個人工作過的人告訴我爲什麼會出現這個問題。無法發送短信使用nexmo或thilio

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行表示該線 客戶=新NexmoSmsClient(API_KEY,API_SECRET);

回答

1

你爲什麼試圖使用庫?只要您知道承運人,您通常可以通過電子郵件發送短信。通常是[email protected]或類似的東西。

下面是與運營商的地址列表網站:http://20somethingfinance.com/how-to-send-text-messages-sms-via-email-for-free/

+0

我想從網頁上發送短信application.So請告訴我如何做? – SpringLearner

+0

有許多原因使用專用的API而不是SMS電子郵件網關。關閉我的頭頂:通過電子郵件網關發送的消息通常不能可靠傳送,您無法使用電子郵件網關接收傳入的SMS消息。 –

0

看起來LIB使用的是有你沒拷貝過來的外部依賴性Nexmo。

從Nexmo library documentation page

安裝

您需要的nexmo-sdk.jar複製到你的應用程序,並確保 它在你的類路徑中。此外,還有一些庫/ lib目錄下的依賴庫 必須複製到您的 應用程序類路徑(如果它們尚未存在)。

所以,看在/lib目錄的壓縮文件下載了SDK,並複製這些地方你把nexmo-sdk.jar文件