2012-07-21 174 views
0

下面的代碼用於偵聽傳入的短信,將短信中的所有空格取出,然後通過電子郵件發送編輯後的短信。一切工作正常,除了應用程序未能發送電子郵件。任何人都可以看到我做錯了什麼,幫助我嗎?電子郵件無法發送短信的詳細信息

new Thread() { 
    public void run() { 

     try { 

      DatagramConnection _dc = 
       (DatagramConnection)Connector.open("sms://"); 

      for(;;) { //'For-Loop' used to listen continously for incoming sms's 

       Datagram d = _dc.newDatagram(_dc.getMaximumLength());      
       _dc.receive(d); //The sms is received       
       byte[] bytes = d.getData(); 
       String address = d.getAddress(); //The address of the sms is put on a string. 
       String msg = new String(bytes); //The body of the sms is put on a string. 
       String msg2 = (replaceAll(msg, " ","")) ; // 


       Store store = Session.getDefaultInstance().getStore(); 

       Folder[] folders = store.list(Folder.SENT); 
       Folder sentfolder = folders[0]; //Retrieve the sent folder 

       Message in = new Message(sentfolder); 
       Address recipients[] = new Address[1]; 

       recipients[0]= new Address("[email protected]", "user"); 

       in.addRecipients(Message.RecipientType.TO, recipients); 

       in.setSubject("Incoming SMS"); //The subject of the message is added 
       in.setContent("You have just received an SMS from: " + address + "/n" + "Message: " + msg2); //Here the body of the message is formed 

       in.setPriority(Message.Priority.HIGH); //The priority of the message is set. 

       Transport.send(in); //The message is sent 

       in.setFlag(Message.Flag.OPENED, true); 
       Folder folder = in.getFolder(); //The message is deleted from the sent folder 
       folder.deleteMessage(in); 
      }  

     }catch (Exception me) { //All Exceptions are caught  
     }  
    } 
}; 

public static String replaceAll(String front, String pattern, String back) { 

    if (front == null) 
    return ""; 

    StringBuffer sb = new StringBuffer(); //A StringBufffer is created 
    int idx = -1; 
    int patIdx = 0; 

    while ((idx = front.indexOf(pattern, patIdx)) != -1) { 
     sb.append(front.substring(patIdx, idx)); 
     sb.append(back); 
     patIdx = idx + pattern.length(); 
    } 

    sb.append(front.substring(patIdx)); 
    return sb.toString(); 
} 

感謝

+0

「無法發送電子郵件」 - 出了什麼問題?有什麼症狀?你有吃的例外嗎?你嘗試過調試嗎? =) – 2012-07-21 09:07:48

+0

有沒有例外,應用程序建立和編譯就好,完美打開。有趣的是,當我離開'String msg2 =(replaceAll(msg,「,」「))'out並將電子郵件內容更改爲msg而不是msg2時,該應用程序確實發送了一封包含短信內容的電子郵件,但我希望代碼通過電子郵件發送編輯後的短信而不用空格。 – BirthOfTragedy 2012-07-21 09:17:51

+0

你在一個網絡連接的設備上,或者模擬器......? – Nate 2012-07-21 09:32:16

回答

0

這是不是一個真正的答案的問題,只是在我上面的評論的闡述,這可能幫助。

請確保在異常捕獲塊中執行某些操作,以免代碼中的問題不被注意。有可能您的代碼沒有遇到任何異常,但爲了幫助我們,我們需要嘗試消除潛在的問題,並且由於您說代碼不起作用,但您有一個空的異常處理程序,這很容易首先要修復的區域。

最簡單的處理程序只是:

try { 
    // try sending sms here 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

如果你能在調試器(我強烈建議)運行它,那麼你現在可以穿上e.printStackTrace()線斷點,看它是否曾經受到打擊。如果是,請檢查e的值並告訴我們它是什麼。通常,在我的程序中,我實際上並沒有在catch處理程序中使用e.printStackTrace(),但是我有一個記錄類,它需要字符串,並且可能是日誌級別(例如info,warning,error,verbose),並寫入一個日誌文件。日誌文件可以附加到用戶發送給技術支持的電子郵件中,或者如果您只想在開發過程中使用該功能,則可以禁用該日誌文件以進行生產。

無論如何,從一個簡單的printStackTrace()開始,看看它是否被擊中。然後,回報。


編輯:從你在你的問題後的評論描述的症狀,好像這是一種可能性,即

String msg2 = (replaceAll(msg, " ","")) ; // 

是拋出一個異常,因此,從來沒有讓你去的地方你會發送電子郵件。初次檢查時,我看不出有什麼不妥,但是這可能是一個看起來很不錯的地方。這個實施是否完全通過了單元測試?

此外,我認爲你的代碼中有一個"/n",你可能需要"\n",對吧?

相關問題