ckeditorjakarta-mailmime-messagemime-mail

While Mail body being received, how to fetch the Image from multipart body


My application actually has mail send / receive functionalities to handle.

While receiving the mail, i am unable to view the image which is an inline image being sent from outlook.

Can some one help me how can i catch the image and make available always.

I have java code like below,

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

Client side i am using CKEditor to handle email body data.

Thanks a lot.


Solution

  • i got a solution from the example shared below

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

    But, this example explains, how to find the image in body and store. I have also done below to replace src

    ` Pattern htmltag = Pattern.compile("]src=\"[^>]>(.?)"); Pattern link = Pattern.compile("src=\"[^>]\">"); String 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");            
    
        }
    

    `

    And on top of this, i gonna do Base64 encoding so that i dont require store in file system.

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

    With all these i can conclude to say, i got solution for my issue. Thank you.