【问题标题】:multipart/alternative getContent() throws IOException: No contentmultipart/alternative getContent() throws IOException: No content
【发布时间】:2018-02-09 14:36:31
【问题描述】:

我的代码有问题。我尝试从我的邮件服务器获取电子邮件文本。检索邮件后,我分析邮件并尝试获取类型为“text/plain”和/或“text/html”的正文部分。 我使用以下方法来拆分 MimeMultipart:

 private String getTextFromMimeMultipart(MimeMultipart mimeMultipart)  throws MessagingException, IOException{
    String result = "";
    int count = mimeMultipart.getCount();
    BodyPart bodypart;
    String html;
    System.out.println("Number of parts: "+count);
    for (int i = 0; i < count; i++) {
        bodypart=mimeMultipart.getBodyPart(i);
        System.out.println("Type of part "+(i+1)+" "+bodypart.getContentType());

        System.out.println("Class: "+bodypart.getClass());

        if(bodypart.isMimeType("text/plain")){
            try{
                //if(bodypart==null){
                  //  System.out.println("Null in bodypart");
                //}else{    
                    System.out.println((String)bodypart.getContent());//cant get Content!
                //}
            }catch (IOException e){
                System.out.println("IO Exception on attachment"+(i+1)+": "+e+"\n");
            }
            //result=(String) bodypart.getContent();
        }else if (bodypart.isMimeType("text/html")){
            try{
                html = (String) bodypart.getContent();//cant get Content!
                System.out.println(org.jsoup.Jsoup.parse(html).text());
                //result = org.jsoup.Jsoup.parse(html).text();
             }catch (IOException e){
                System.out.println("IOException on part"+(i+1)+": "+e+"\n");
            }

        }else if (bodypart.isMimeType("multipart/*")){
            System.out.println("Openning Multipart bodypart...!\n");
            getTextFromMimeMultipart((MimeMultipart) bodypart.getContent());

        }else{

            System.out.println("No none part?!: "+bodypart.getContentType());
        }
    }
    return result;
}

我的问题在于 .getContent()。它总是抛出 IOException: no Content,尽管我知道我的邮件中有文本。

生成了以下输出:

Trying:***mail Subject***...
-------------------------------------------------------------------------------
Number of parts: 1
Type of part 1 multipart/alternative; 
    boundary=_000_************************************************_(snippedout)
Class: class com.sun.mail.imap.IMAPBodyPart
Openning Multipart bodypart...!

Number of parts: 2
Type of part 1 text/plain; charset=Windows-1252
Class: class com.sun.mail.imap.IMAPBodyPart
IO Exception on attachment1: java.io.IOException: No content

Type of part 2 text/html; charset=Windows-1252
Class: class com.sun.mail.imap.IMAPBodyPart
IOException on part2: java.io.IOException: No content

我错过了一些重要的事情吗?我处理的 bodypart 正确吗?

补充说明: 很抱歉没有提供调用功能。我确实打开了文件夹

调用它的函数:

public void findJunkonBody (String pfolder, String pdestfolder)throws MessagingException, IOException{
    Folder folder ;
    Store store = null;
    Folder destfolder;
    MailUtil mutil = new MailUtil(smtphost,user,pass,"imap");

    String [] junktermsbody=new String[1];
    junktermsbody[0]="Empfangsbestätigung";

    try{
        store=buildIMAPStore(); 
        store.connect(smtphost,user,pass);
        folder = store.getFolder(pfolder);
        destfolder = store.getFolder(pdestfolder);

        folder.open(Folder.READ_WRITE);
        Message[] msgs = folder.getMessages();
        int i;
        int j;
        MimeMultipart mmpart;
        for (i=0; i<=(msgs.length-1);i++){
            for(j=0;j<=(junktermsbody.length-1);j++){
                if (msgs[i].isMimeType("text/plain")){
                    if(msgs[i].getContent().toString().contains(junktermsbody[j])){
                        System.out.println("Palin text: "+msgs[i].getSubject());
                        //move logik
                    }
                }else if (msgs[i].isMimeType("multipart/*")){
                    mmpart =(MimeMultipart) msgs[i].getContent();
                    System.out.println("Trying:"+msgs[i].getSubject()+"...\n-------------------------------------------------------------------------------");
                    if (getTextFromMimeMultipart(mmpart).contains(junktermsbody[j])){
                        System.out.println("Time to move it!");
                        //move logik
                    }
                }
            }
        }

        folder.close(true);
    }finally{
        if (store != null) {store.close();}
        //System.out.println("Junk on Body moved successfully"); 
        System.out.println();
    }

}

【问题讨论】:

    标签: java email


    【解决方案1】:

    对于纯文本/文本消息,您需要先打开 java 服务器邮件文件夹。 阅读内容前需要打开文件夹。

    Folder folder = message.getFolder();
    // Open folder in read-only mode
    if (folder.isOpen()) {
        if ((folder.getMode() & Folder.READ_WRITE) != 0) {
            folder.close(false);
            folder.open(Folder.READ_ONLY);
        }
    } else {
        folder.open(Folder.READ_ONLY);
    }
    

    现在您可以阅读邮件正文内容了:

    Object content = message.getContent();
    

    【讨论】:

    • 使用较新版本的 javax.mail 后,我解决了 IO 异常问题。但现在 getContent() 返回一个空字符串。所以我仍然无法获得我的内容。我确实打开了文件夹,所以这不是问题。
    猜你喜欢
    • 2013-09-02
    • 1970-01-01
    • 2018-07-26
    • 2015-02-07
    • 2012-08-20
    • 2018-08-30
    • 2012-01-09
    • 2013-07-09
    相关资源
    最近更新 更多