【问题标题】:Extracting inline images coming in mail body using java Mail API使用 java Mail API 提取邮件正文中的内联图像
【发布时间】:2020-03-14 21:44:45
【问题描述】:

我编写了两种方法,一种用于附件,另一种用于电子邮件正文内容。我需要从电子邮件正文中提取图像。这两种方法工作正常,但是当图像进入电子邮件正文时,它应该保存在数据库中。所以以后可以用。

附件:- public static List getAttachments(MimeMultipart multipart, List existingAttachments) {

    if (multipart != null) {
        try {
            if (existingAttachments == null) {
                existingAttachments = new ArrayList<MimeBodyPart>();
            }
            for (int i = 0; i < multipart.getCount(); i++) {
                if (multipart.getBodyPart(i) instanceof MimeBodyPart) {
                    MimeBodyPart currentPart = (MimeBodyPart) multipart.getBodyPart(i);
                    if (Part.ATTACHMENT.equalsIgnoreCase(currentPart.getDisposition())) {
                        if (!existingAttachments.contains(currentPart)) {
                            existingAttachments.add(currentPart);
                            System.out.println(currentPart.getFileName());
                        }
                    } else if (currentPart.getContent() instanceof MimeMultipart) {
                        existingAttachments = getAttachments((MimeMultipart) currentPart.getContent(), existingAttachments);
                    }
                }
            }
        } catch (MessagingException ex) {
            LoggerFactory.getLogger(EmailUtil.class.getName()).error(ex.getMessage());
        } catch (IOException ex) {
            LoggerFactory.getLogger(EmailUtil.class.getName()).error(ex.getMessage());
        }
    }
    return existingAttachments;
}

用于电子邮件正文内容此方法正在提取电子邮件正文内容

public static String getContent(MimeMultipart multipart) {
    String emailContent = null;
    if (multipart != null) {
        try {
            for (int i = 0; i < multipart.getCount(); i++) {
                if (multipart.getBodyPart(i) instanceof MimeBodyPart) {
                    MimeBodyPart currentPart = (MimeBodyPart) multipart.getBodyPart(i);
                    if (Part.INLINE.equalsIgnoreCase(currentPart.getDisposition())) {
                        LoggerFactory.getLogger(EmailUtil.class.getName()).info("Content dispo is inline");
                        emailContent = (String) currentPart.getContent();
                    } else if (currentPart.getDisposition() == null && currentPart.getContentType().toLowerCase().contains("text")) {
                        LoggerFactory.getLogger(EmailUtil.class.getName()).info("Content dispo is null and type is text/*");
                        try {
                           emailContent = (String) currentPart.getContent();

                        } catch (ClassCastException ex) {
                            LoggerFactory.getLogger(EmailUtil.class.getName()).warn("Classcast exception caught and managed");
                            try {
                                InputStream is = currentPart.getInputStream();

                                emailContent = IOUtils.toString(is, currentPart.getEncoding());
                                Document doc=Jsoup.parse(emailContent);
                                Elements elements =doc.getElementsByTag("img");
                                System.out.println(elements);
                                int htmlCloseIndex = emailContent.indexOf("</html>");
                                emailContent = emailContent.substring(0, htmlCloseIndex);
                                emailContent+="</html>";
                            } catch (Exception e) {
                                LoggerFactory.getLogger(EmailUtil.class.getName()).error("Exception rebound caught and managed, email content will not read");
                                //emailContent = "Unable to read email content";
                                e.printStackTrace();
                            }
                        }
                    }else if (currentPart.getDisposition() == null && currentPart.getContentType().contains("TEXT")) {
                        LoggerFactory.getLogger(EmailUtil.class.getName()).info("Content dispo is null and type is TEXT/*");
                        try {
                            emailContent = (String) currentPart.getContent();
                        } catch (ClassCastException ex) {
                            LoggerFactory.getLogger(EmailUtil.class.getName()).warn("Classcast exception caught and managed");
                            try {
                                InputStream is = currentPart.getInputStream();
                                emailContent = IOUtils.toString(is, currentPart.getEncoding());

                                int htmlCloseIndex = emailContent.indexOf("</html>");
                                emailContent = emailContent.substring(0, htmlCloseIndex);
                                emailContent+="</html>";
                            } catch (Exception e) {
                                LoggerFactory.getLogger(EmailUtil.class.getName()).error("Exception rebound caught and managed, email content will not read");
                                //emailContent = "Unable to read email content";
                                e.printStackTrace();
                            }
                        }
                    } 
                    else if (currentPart.getContent() instanceof MimeMultipart) {
                        emailContent = getContent((MimeMultipart) currentPart.getContent());

                    }
                }
            }
        } catch (MessagingException ex) {
            LoggerFactory.getLogger(EmailUtil.class.getName()).error(ex.getMessage());
            LoggerFactory.getLogger(EmailUtil.class.getName()).warn("email content will not read");
           //emailContent = "Unable to read email content";
        } catch (IOException ex) {
            LoggerFactory.getLogger(EmailUtil.class.getName()).error(ex.getMessage());
            LoggerFactory.getLogger(EmailUtil.class.getName()).warn("email content will not read");
           // emailContent = "Unable to read email content";
        } catch (ClassCastException ex) {
            LoggerFactory.getLogger(EmailUtil.class.getName()).warn("Classcast exception caught and managed");
        // emailContent = "Unable to read email content";
        }
    }
    return emailContent;
}

【问题讨论】:

  • 你的问题不清楚。你能具体说明你真正需要帮助的是什么吗?我可以想到您可能意味着的多个问题:如何获取图像的字节,如何将图像数据存储在数据库中。作为旁注/不是您问题的中心:在我看来,当您有一个 instanceof 运算符可以用来测试对象是否是类的一部分时,捕获 classcastexception 是不好的做法。
  • 是的,我想获取图像的字节并想存储在我的本地目录中。

标签: java jakarta-mail


【解决方案1】:

好的,从&lt;img src="..."&gt;标签开始,你已经拿走了:

Elements elements = doc.getElementsByTag("img");

嵌入图像的img 标记如下所示:

<img src="data:image/jpeg;base64,..." ...>

所以拥有src 属性:

String src = ...
if (src.startsWith("data:")) { // Embedded image data.
    int p = src.indexOf(';'); // Or better ";base64,"
    if (p == -1) {
        throw new IllegalArgumentException();
    }
    String mimeType = src.substring(5, p);
    String base64Data = src.substring(p + 1 + 6 + 1); // ";base64,"
    byte[] data = Base64.getDecoder().decode(base64Data);
    String file = "test." + mimeType.replaceFirst("^.*/(.*)$", "$1");
    Path path = Paths.get(file);
    Files.write(path, data);
}

【讨论】:

  • 我不会这样...对我来说,我正在提取电子邮件正文并获取 像这样的标签
  • 那个 cid: 协议显示了将图像作为附件的替代方案,这更简单。 blog.mailtrap.io/… - 简单地获取带有该 cid 的附件。
猜你喜欢
  • 2023-03-29
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2018-02-26
  • 2018-04-11
  • 2015-05-13
相关资源
最近更新 更多