Question

Help please to solve my problem. I have a function that find email content and attachments:

public void setContentAndAttachmentsList(GMailEmail gMailEmail, Part message) throws MessagingException, IOException {
    if (message.isMimeType("text/*") && StringUtils.isEmpty(message.getFileName())) {
        gMailEmail.setContent(message.getContent().toString());
        log.debug("Found text data. Set as body: " + message.getContent().toString());
    } else if (message.isMimeType(MULTIPART_MIME_TYPE)) {
        Multipart multipartMessage = (Multipart) message.getContent();
        for (int i = 0; i < multipartMessage.getCount(); i++) {
            BodyPart messagePart = multipartMessage.getBodyPart(i);
            setContentAndAttachmentsList(gMailEmail, messagePart);
        }
    } else if (StringUtils.isNotBlank(message.getFileName())) {
        MailAttachment attachment = new MailAttachment();
        attachment.setContentType(message.getContentType());
        log.debug("Attachment content type: " + message.getContentType());
        attachment.setName(message.getFileName());
        log.debug("Attachment file name: " + message.getFileName());
        if (message.getContent() instanceof InputStream) {
            attachment.setInputStream((InputStream) message.getContent());
        } else {
            attachment.setInputStream(IOUtils.toInputStream(message.getContent().toString(), UTF_8));
        }
        List<MailAttachment> attachmentsList = gMailEmail.getAttachmentsList();
        attachmentsList.add(attachment);
        gMailEmail.setAttachmentsList(attachmentsList);
        log.debug("Found attachment" + message.getFileName());
    }
}

GMailEmail is POJO. If email has attachments that have extensions it works perfectly. But when attachments have no extensions, message.getFileName() simply returns null.

Was it helpful?

Solution

I solved this problem. It must be

StringUtils.isBlank(message.getFileName())

instead of

StringUtils.isEmpty(message.getFileName())

in second line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top