【问题标题】:JavaMail - Parsing email content, can't seem to get it to work! (Message.getContent())JavaMail - 解析电子邮件内容,似乎无法正常工作! (消息.getContent())
【发布时间】:2011-08-03 11:00:22
【问题描述】:

几周以来,我一直在为 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”之类的乱码。

谁能看出我哪里出错了?

谢谢, 里斯

【问题讨论】:

    标签: java android jakarta-mail


    【解决方案1】:

    以上建议均无效。你不需要在这里做任何复杂的事情。 Mimemessage 已获得message.writeTo(outputStream);

    您只需要打印消息:

    message.writeTo(System.out);
    

    上面的代码会将实际的 mime 消息打印到控制台(或者您可以使用任何记录器)。

    将内容保存到.eml,即可在outlook中打开。就这么简单!

    【讨论】:

    • 谢谢你。这绝对是正确的解决方案!
    【解决方案2】:
        Multipart multipart = (Multipart) content;
        BodyPart part = multipart.getBodyPart(0);
        part.toString();
        contentReturn = part.getContent().toString();
    

    当你有 BodyPart 部分时,你应该继续测试

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

    【讨论】:

      【解决方案3】:

      我在解析 javax 邮件的 Message 时遇到了同样的问题。在我的解决方法中,我发现了一件奇怪的事情。列出来自 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;
      
      }
      

      希望对某人有所帮助。

      【讨论】:

        【解决方案4】:

        我几乎每件事都尝试过同样的错误,但对我有用的解决方案是

        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();
            }
        

        希望这会对某人有所帮助。

        【讨论】:

          【解决方案5】:

          不完全确定,但听起来您想将 MimeMessage 转换为字符串。严格来说,没有将 MimeMessage 转换为 String 的“标准”翻译,但这是我几年前编写的一段代码,它试图生成一个这样的翻译。仅针对英文消息进行测试,因此 i18n 必须是您自己考虑的事情。

          不幸的是,SO 的愚蠢过滤器似乎认为我的代码示例是一张图片,不会让我发布它:看看http://pastebin.com/pi9u5Egq 的课程:代码正在做一堆其他不必要的事情(它是将其吐出为使用飞碟呈现的 HTML),还需要 Jakarta Commons Lang、javamail 和激活库,但它对我有用。你可以这样调用它:

          fetchText(message, null, false, false);

          获取文本字符串。试试看大小。

          【讨论】:

          • 我编译了我的应用程序,不知何故大多数电子邮件都被解析了......然后当我重新启动应用程序时,我收到了返回,显然我的电子邮件收件箱是空的。 :( 所以如果我能解决这个问题,我会试试那个代码 :D (新问题.. 如果你有兴趣:stackoverflow.com/questions/4652946/…
          猜你喜欢
          • 2022-08-03
          • 2014-03-03
          • 2016-02-19
          • 2012-07-12
          • 2017-09-04
          • 2012-05-06
          • 2013-02-17
          • 2017-10-09
          相关资源
          最近更新 更多