电子邮件无法发送短信详细信息

时间:2012-07-21 09:03:50

标签: email blackberry sms

以下代码侦听传入的短信,从短信中取出所有空格,然后通过电子邮件发送已编辑的短信。一切正常,但应用程序无法发送电子邮件。任何人都可以看到我做错了什么并帮助我吗?

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("me@yahoo.com", "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();
}

由于

1 个答案:

答案 0 :(得分:0)

这不是问题的答案,只是对上述评论的详细说明,这可能有所帮助。

确保在异常catch块中执行某些操作,以便代码中的问题不会被忽视。您的代码可能没有遇到任何异常,但为了让我们能够提供帮助,我们需要尝试消除潜在的问题,因为您说代码不起作用,但是您有一个空的异常处理程序,这很容易首先要修复的区域。

最简单的处理程序就是:

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

如果您可以在调试器中运行它(我强烈建议),那么您现在可以在e.printStackTrace()行上放置一个断点,看看它是否被击中。如果是,请检查e的值并告诉我们它是什么。

通常,在我的程序中,我实际上并没有在catch处理程序中使用e.printStackTrace(),但我有一个记录类,它接受字符串,也许是日志级别(例如信息,警告,错误,详细),并写入日志文件。日志文件可以附加到用户发送给技术支持的电子邮件中,或者如果您只想在开发时使用该功能,则可以禁用该日志文件。

无论如何,从一个简单的printStackTrace()开始,看它是否被击中。然后,报告回来。


编辑:根据您在问题后的评论中描述的症状,似乎有可能

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

正在抛出异常,因此永远不会让您到达您发送电子邮件的位置。在初步检查时,我对replaceAll()的实施没有任何问题,但这可能是一个值得关注的地方。该实施是否经过了彻底的单元测试?

此外,我认为您的代码中有"/n",您可能需要"\n",对吧?

相关问题