0

最近我在通过 oracle-javamail 从我的电子邮件中下载 CSV 时遇到了问题。我发现第一个工作正常(来自大型机服务器)但是第二个 csv 附件似乎作为“application/vnd.ms-excel”出现,因为它显然是一个 .csv 文件,就像另一个文件一样下载完全没问题。

我对它们都使用了相同的代码,但找不到有关此问题的任何信息。

如果我将那封电子邮件转发给自己,我可以神奇地下载附件而不会出现任何问题。任何帮助,将不胜感激。一旦我完成这项工作,另一项日常任务就会自动化!

    if(content instanceof java.lang.String){
    //emailBody = (String)content;
} else if(content instanceof Multipart){
    System.out.println("is Multipart");
    Multipart mp = (Multipart)content;
    for (int j=0; j < mp.getCount(); j++) {
        Part part = mp.getBodyPart(j);
        String disposition = part.getDisposition();
        if (disposition == null) {
            // Check if plain
            MimeBodyPart mbp = (MimeBodyPart)part;
            if (mbp.isMimeType("text/plain")) {                             
                //emailBody += (String)mbp.getContent();
            } else {                            
                // Special non-attachment cases here of 
                // image/gif, text/html, ...                                
                String fileName = decodeName(part.getFileName());
                File savedir = new File(path+"\\"+fileName);
                saveFile(savedir, part);                                
            }
        } else if ((disposition != null) &&  
                (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))){
                // Check if plain
                MimeBodyPart mbp = (MimeBodyPart)part;
                if (mbp.isMimeType("text/plain")) {                             
                    System.out.println("has disp of plain");
                    //emailBody += (String)mbp.getContent();
                } else {                            
                    System.out.println("has disp of else");
                    String fileName = decodeName(part.getFileName());
                    File savedir = new File(path+"\\"+fileName);
                    saveFile(savedir, part);                    
                }
        }
    }
}
else{
    System.out.println(message.getContentType());
}  
4

1 回答 1

0

Although it appears that java knows what type of email it is receiving it turns out that it doesn't. Since this isn't a standard type it will return it as an Input Stream. I was able to see the contents of the file with the below code.

Also note that the only reason this came over as a application/vnd.ms-excel file type is because the body of the email was empty.

        InputStream emailIn = (InputStream) message.getContent();
    Scanner s = null;
    try{
        s = new Scanner(emailIn).useDelimiter("\\A");
    while(s.hasNext())
        System.out.println(s.next());
    }
    finally{
        s.close();
    }
于 2013-02-18T14:00:28.973 回答