Umair
Umair

Reputation: 3253

IMAP body command. How to get just the text or html part of the body?

I have a query regarding the FETCH...BODY[...] command.

What is the best way to just obtain the text or html part of the body? I thought that the BODY[TEXT] command would do this but it is also returning attachments etc which is severely affecting performance.

Currently, this is what I do (C# code):

        if (contentType != null)
        {
            switch (contentType.ToLower())
            {
                case "multipart/alternative":
                case "text/plain":
                case "text/html":
                    body = " BODY[1]";
                    break;
                case "multipart/related":
                case "multipart/signed":
                    body = " BODY[1.1]";
                    break;
                case "multipart/report":
                    body = " BODY[1]";
                    break;
                case "multipart/mixed":
                    if (mail.MailBody.TextBodyPartCount == 1)
                        body = " BODY[1]";
                    else if (mail.MailBody.TextBodyPartCount == 2)
                    {
                        if (bodyType == BodyType.Plain)
                            body = " BODY[1.1]";
                        else
                            body = " BODY[1.2]";
                    }
                    else
                        body = " BODY[1]";
                    break;
                default:
                    body = " BODY[1]";
                    break;
            }
        }
        else
        {
            body = " BODY[1]";
        }

This works most of the time, but in some cases it returns NIL. Sorry if I have left any details out, but please do ask!

Thanks.

Upvotes: 1

Views: 2522

Answers (1)

Pawel Lesnikowski
Pawel Lesnikowski

Reputation: 6391

Email messages are stored in MIME format - it allows creating a tree structure with unlimited depth. You can't assume how this tree structure looks like by only checking content-type.

You should use BODYSTRUCTURE to recreate the tree structure without downloading the message, and after that you can decide which parts you need to fetch.

Upvotes: 1

Related Questions