簡體   English   中英

JavaMail - 解析電子郵件內容,似乎無法讓它工作! (Message.getContent())

[英]JavaMail - Parsing email content, can't seem to get it to work! (Message.getContent())

幾個星期以來,我一直在為android開發一個電子郵件客戶端,我一直忽略解析電子郵件內容一段時間,因為我從來沒有能夠讓它工作。 因此,現在是時候尋求幫助了!

我一直在環顧四周,我遇到過一些我嘗試過的方法,但從來沒有取得多大成功! 目前我最接近的嘗試必須是:

private String parseContent(Message m) throws Exception
{       
    //Multipart mp = (Multipart)c;
    //int j = mp.getCount();

    /*for (int i = 0; i < mp.getCount(); i++)
    {
        Part part = mp.getBodyPart(i);
        System.out.println(((MimeMessage)m).getContent());
        content = content + part.toString();
        //System.out.println((String)part.getContent());
    }*/

    Object content = m.getContent();
    String contentReturn = null;

    if (content instanceof String) 
    {
        contentReturn = (String) content;
    } 
    else if (content instanceof Multipart) 
    {
        Multipart multipart = (Multipart) content;
        BodyPart part = multipart.getBodyPart(0);
        part.toString();
        contentReturn = part.getContent().toString();
    }   
    return contentReturn;
}

但它不起作用,我得到像“javax.mail.internet.MimeMultipart@44f12450”這樣的胡言亂語。

任何人都可以看到我錯在哪里?

謝謝,里斯

以上建議均無效。 你不需要在這里做任何復雜的事情。 Mimemessage有message.writeTo(outputStream);

您打印郵件所需的只是:

message.writeTo(System.out);

上面的代碼會將實際的mime消息打印到控制台(或者你可以使用任何記錄器)。

將內容保存到.eml ,您可以在Outlook中打開它。 就那么簡單!

    Multipart multipart = (Multipart) content;
    BodyPart part = multipart.getBodyPart(0);
    part.toString();
    contentReturn = part.getContent().toString();

當你有BodyPart部分時,你應該繼續測試

if(part.getContent()instanceof String){...}

我也得到了同樣的錯誤,幾乎每件事都嘗試過,但解決方案對我有用

private String getFinalContent(Part p) throws MessagingException,
            IOException {

        String finalContents = "";
        if (p.getContent() instanceof String) {
            finalContents = (String) p.getContent();
        } else {
            Multipart mp = (Multipart) p.getContent();
            if (mp.getCount() > 0) {
                Part bp = mp.getBodyPart(0);
                try {
                    finalContents = dumpPart(bp);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return finalContents.trim();
    }

private String dumpPart(Part p) throws Exception {

        InputStream is = p.getInputStream();
        // If "is" is not already buffered, wrap a BufferedInputStream
        // around it.
        if (!(is instanceof BufferedInputStream)) {
            is = new BufferedInputStream(is);
        }
        return getStringFromInputStream(is);
    }

private String getStringFromInputStream(InputStream is) {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

希望這會對某人有所幫助。

解析javax郵件時遇到了同樣的問題。 在我的解決方法中,我發現了一個奇怪的事情。 從POP3列出郵件並沒有給我郵件正文內容。 所以使用IMAP對我有用。 現在我能夠解析Text / plain以及Text / Html並閱讀正文。 要解析相同的我使用以下方法。

public String printMessage(Message message) {

    String myMail = "";

    try {
        // Get the header information
        String from = ((InternetAddress) message.getFrom()[0])
                .getPersonal();



        if (from == null)
            from = ((InternetAddress) message.getFrom()[0]).getAddress();
        System.out.println("FROM: " + from);
        String subject = message.getSubject();
        System.out.println("SUBJECT: " + subject);
        // -- Get the message part (i.e. the message itself) --
        Part messagePart = message;
        Object content = messagePart.getContent();
        // -- or its first body part if it is a multipart message --
        if (content instanceof Multipart) {
            messagePart = ((Multipart) content).getBodyPart(0);
            System.out.println("[ Multipart Message ]");
        }
        // -- Get the content type --
        String contentType = messagePart.getContentType();
        // -- If the content is plain text, we can print it --
        System.out.println("CONTENT:" + contentType);
        if (contentType.startsWith("TEXT/PLAIN")
                || contentType.startsWith("TEXT/HTML")) {
            InputStream is = messagePart.getInputStream();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is));
            String thisLine = reader.readLine();
            while (thisLine != null) {
                System.out.println(thisLine);
                myMail = myMail + thisLine;
                thisLine = reader.readLine();
            }


        }
        System.out.println("-----------------------------");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return myMail;

}

希望它可以幫助某人。

不完全確定,但聽起來你想將MimeMessage轉換為String。 嚴格來說,MimeMessage to String沒有“標准”翻譯,但是這是我幾年前寫的一段代碼,試圖生成一個這樣的翻譯。 只測試了英文消息,所以i18n必須是你自己想到的東西。

不幸的是,SO的愚蠢過濾器似乎認為我的代碼示例是一個圖像,不會讓我發布它:看看http://pastebin.com/pi9u5Egq上的類:代碼正在做一堆其他不必要的事情(它將它吐出到使用飛碟渲染的HTML中),還需要Jakarta Commons Lang,javamail和激活庫,但它適用於我。 你會像這樣調用它:

fetchText(message, null, false, false);

獲取文本字符串。 嘗試大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM