【问题标题】:While Mail body being received, how to fetch the Image from multipart body收到邮件正文时,如何从多部分正文中获取图像
【发布时间】:2018-11-06 16:57:07
【问题描述】:

我的应用程序实际上具有要处理的邮件发送/接收功能。

在收到邮件时,我无法查看从 Outlook 发送的内嵌图像。

谁能帮助我如何捕捉图像并始终可用。

我有如下的java代码,

try (InputStream stream = new ByteArrayInputStream(Base64
            .getMimeDecoder().decode(mail))) {

        MimeMessage message = new MimeMessage(null, stream);
        Object messageContent = message.getContent();
        if (messageContent instanceof String) {
            body = (String) messageContent;
        } else if (messageContent instanceof MimeMultipart) {
            content = (MimeMultipart) messageContent;
            for (int i = 0; i < content.getCount(); i++) {
                BodyPart bodyPart = content.getBodyPart(i);
                String disposition = bodyPart.getDisposition();

                if (disposition == null
                        || disposition
                                .equalsIgnoreCase(Part.INLINE)) {
                    Object object = bodyPart.getContent();
                    if (object instanceof String) {
                        body = object.toString();
                    } else if (object instanceof MimeMultipart) {
                        MimeMultipart mimeMultipart = (MimeMultipart) object;
                        String plainBody = null;
                        String htmlBody = null;

                        for (int j = 0; j < mimeMultipart.getCount(); j++) {
                            BodyPart multipartBodyPart = mimeMultipart
                                    .getBodyPart(j);
                            String multipartDisposition = multipartBodyPart
                                    .getDisposition();
                            String multipartContentType = multipartBodyPart
                                    .getContentType();
                            if (multipartDisposition == null
                                    && multipartContentType != null) {
                                if (multipartContentType
                                        .contains(MediaType.TEXT_HTML)) {
                                    htmlBody = multipartBodyPart
                                            .getContent().toString();
                                } else if (multipartContentType
                                        .contains(MediaType.TEXT_PLAIN)) {
                                    plainBody = multipartBodyPart
                                            .getContent().toString();
                                }
                            }
                        }
                        if (htmlBody != null) {
                            body = htmlBody;
                        } else {
                            body = plainBody;
                        }
                    }
                }
            }
        }

客户端我正在使用 CKEditor 来处理电子邮件正文数据。

非常感谢。

【问题讨论】:

    标签: ckeditor jakarta-mail mime-message mime-mail


    【解决方案1】:

    我从下面分享的示例中得到了解决方案

    https://www.tutorialspoint.com/javamail_api/javamail_api_fetching_emails.htm

    但是,这个例子解释了如何在 body 和 store 中找到图像。 我也在下面做了替换 src

    ` 模式 htmltag = Pattern.compile("]src=\"[^>]>(.?)"); 模式链接 = Pattern.compile("src=\"[^>]\">"); 字符串 s1 = "";

        Matcher tagmatch = htmltag.matcher(s1);
        List<String> links = new ArrayList<String>();
        while (tagmatch.find()) {
            Matcher matcher = link.matcher(tagmatch.group());
            matcher.find();
            String link1 = matcher.group().replaceFirst("src=\"", "")
                    .replaceFirst("\">", "")
                    .replaceFirst("\"[\\s]?target=\"[a-zA-Z_0-9]*", "");
            links.add(link1);
            s1 = s1.replaceAll(link1, "C:\\//Initiatives_KM\\//image.jpg");            
    
        }
    

    `

    除此之外,我将进行 Base64 编码,这样我就不需要存储在文件系统中。

    encodedfileString = Base64.getEncoder().encodeToString(bArray);

    有了所有这些,我可以总结说,我找到了解决问题的方法。谢谢。

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 2019-01-18
      相关资源
      最近更新 更多