使用JavaMail读取电子邮件附件-读取“ winmail.dat”而不是实际附件

时间:2018-11-14 18:45:41

标签: java javamail apache-tika

我正在尝试构建一个使用javax.mail API来读取电子邮件(包括附件)的客户端。我最初的想法是使用Tika库。当产生意外结果时(请参阅下面的代码和详细信息),我尝试使用this question中提供的代码的变体。

我的阅读电子邮件功能:

    // Reference: https://www.tutorialspoint.com/javamail_api/javamail_api_checking_emails.htm
public class CheckingMails {

    public static void check(String host, String storeType, String user, String password) {
        try {

            // create properties field
            Properties properties = new Properties();

            properties.put("mail.pop3.host", host);
            properties.put("mail.pop3.port", "110");
            Session emailSession = Session.getInstance(properties);
            Store store = emailSession.getStore("pop3");
            store.connect(host, user, password);
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_WRITE);

            // retrieve the messages from the folder in an array and print it
            Message[] messages = emailFolder.getMessages();

            System.out.println("messages.length---" + messages.length);

            for (int i = 0, n = messages.length; i < n; i++) {
                Message message = messages[i];

                Object content = message.getContent();
                if (content instanceof java.lang.String) {
                    System.out.println((String) content);
                } else if (content instanceof 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")) {
                                System.out.println("Mime type is plain");
                                System.out.println((String) mbp.getContent());
                            } else {

                                // Special non-attachment cases here of
                                // image/gif, text/html, ...

                                System.out.println("File name is " + part.getFileName());

                            }
                        } 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("Mime type is plain");
                                System.out.println((String) mbp.getContent());
                            } else {
                                System.out.println("Save file " + part.getFileName());

                            }
                        }
                    }
                }

                System.out.println("Tika results " + parseMsg(message));

            }

            // close the store and folder objects
            emailFolder.close(false);
            store.close();

        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

当我通过发送带有简单CSV附件的测试消息来测试程序时,Tika解析器将输出以下内容(为简洁起见被删节):

    ------=_NextPart_000_0028_01D47C16.81FB45C0
    Content-Type: application/ms-tnef;
        name="winmail.dat"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;
        filename="winmail.dat"

    eJ8+IhUSAQaQCAAEAAAAAAABAAEAAQeQBgAIAAAA5AQAAAAAAADoAAEIgAcAGAAAAElQTS5NaWNy
    b3NvZnQgTWFpbC5Ob3RlADEIAQOQBgCgDQAANgAAAAsAAgABAAAAAwAmAAAAAAALACkAAAAAAB4A
    cAABAAAABQAAAFRlc3QAAAAAAgFxAAEAAAAWAAAAAdR8SMswwghurPF+SpeO8uRrTzrhCgAACwAB
    DgAAAAACAQoOAQAAABg..... 

------=_NextPart_000_0028_01D47C16.81FB45C0--

代码的另一部分(检查消息的处置),输出类似于以下内容:

 messages.length---1
Mime type is plain



Email Boday



Save file winmail.dat

为什么应用程序读取/识别winmail.dat,而不是与消息(CSV文件)一起实际发送的附件?

感谢您的帮助。

0 个答案:

没有答案