5

I have tried to find some kind of answer or even a hint on how to do this, but, with no luck.

I want to check if email message has any attachments. But if I use suggested code:

Object content = bPart.getContent();

if (content instanceof String) {
    if (Part.ATTACHMENT.equalsIgnoreCase(bPart.getDisposition()) || StringUtils.isNotBlank(bPart.getFileName())) {
        // It's attachment
        haveAttachment = true;
    }
    else {
        // It's text or html
        emailBody = content.toString();
    }
}
else if (content instanceof InputStream) {
    if (Part.ATTACHMENT.equalsIgnoreCase(bPart.getDisposition()) || StringUtils.isNotBlank(bPart.getFileName())) {
        // It's attachment
        haveAttachment = true;
    }
}

I must use getContent() function on my email message and message is automatically marked as SEEN (read) on the server.

Can anybody help me how to write a simple function to get basic email message info and display if message has any attachments without having to get the whole message content? Actually, I need help with attachments only, because I already know how to get other basic header fields and they do not request getContent() method and they do not mark my email as SEEN.

How does a regular email client does this? I guess there must be some fast and simple way to check if email has attachments.

Thank you in advance!

Edit: Alternatively, is there a way to use getContent() function without marking email as SEEN?

1 Answer 1

3

I don't know where you got that "suggested" code but it certainly wasn't from the JavaMail web site.

You'll want to read this JavaMail FAQ entry.

To check the MIME type of a part, use the isMimeType method. You can find an example in the msgshow.java sample program.

Sign up to request clarification or add additional context in comments.

5 Comments

At this point it is not important to me what is MIME type. I just want to know if there are any attachments. Also, you get suggested code if you implement what they said in their FAQ (you go through the message searching for the part that is attachment).
Checking for the MIME type is how you determine if there is an attachment without actually loading any of the content. As the FAQ explains, "does this message have attachments?" is not a simple question to answer in the general case. If you don't care about the unusual cases, it's a bit simpler.
In order to get MIME type of message I have to get its content first, as shown in their FAQ example "How do I find the main message body in a message that has attachments?". Please, if you now a way to read MIME type of the message (or message part) without getting its content, it would be very helpful to me.
Now, you don't have to get the message content to determine the MIME type. As I said, use the isMimeType method. Did you look at the msgshow.java sample program?
Actually you are correct. I wasn't aware of msgshow.java sample code until now. I will test the code to see if it will work correct in more complicated cases and if it does this is definitely the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.