下面的代碼用於偵聽傳入的短信,將短信中的所有空格取出,然後通過電子郵件發送編輯後的短信。一切工作正常,除了應用程序未能發送電子郵件。任何人都可以看到我做錯了什麼,幫助我嗎?電子郵件無法發送短信的詳細信息
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();
}
感謝
「無法發送電子郵件」 - 出了什麼問題?有什麼症狀?你有吃的例外嗎?你嘗試過調試嗎? =) – 2012-07-21 09:07:48
有沒有例外,應用程序建立和編譯就好,完美打開。有趣的是,當我離開'String msg2 =(replaceAll(msg,「,」「))'out並將電子郵件內容更改爲msg而不是msg2時,該應用程序確實發送了一封包含短信內容的電子郵件,但我希望代碼通過電子郵件發送編輯後的短信而不用空格。 – BirthOfTragedy 2012-07-21 09:17:51
你在一個網絡連接的設備上,或者模擬器......? – Nate 2012-07-21 09:32:16