尝试强制转换javax.mail.multipart时出错

时间:2011-06-17 19:19:40

标签: java javamail

您好我正在编写一个程序来扫描附件的电子邮件并在本地保存这些附件,但我不断收到以下错误(错误信息后的代码跟踪)谢谢

    Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart
    at reader.main(reader.java:70)
                    Multipart multipart =  (Multipart) message[i].getContent();
                    for (int j = 0, m = multipart.getCount(); j < m; i++){
                        Part part = multipart.getBodyPart(j);

                        String disposition = part.getDisposition();

                        if ((disposition != null)&& (disposition.equals(Part.ATTACHMENT)|| (disposition.equals(Part.INLINE)))){
                            saveFile(part.getFileName().toString(), part.getInputStream());

                        }
                    }


    }

    // Close connection 
    folder.close(false);
    store.close();


}
    static void saveFile(String filename, InputStream in){
        File file = new File("c:/temp/" + filename);
        for (int i = 0; file.exists ();i++) {
           file = new File(filename + i);
        }
    }

1 个答案:

答案 0 :(得分:0)

可能这是从以下行中的强制转换为Multipart:

Multipart multipart =  (Multipart) message[i].getContent();

检查getContent的结果是否是对Multipart的有效强制转换。