Esempio n. 1
0
        // A parenthesized list that describes the [MIME-IMB] body
        // structure of a message.
        // Multiple parts are indicated by parenthesis nesting.  Instead
        // of a body type as the first element of the parenthesized list,
        // there is a sequence of one or more nested body structures.  The
        // second element of the parenthesized list is the multipart
        // subtype (mixed, digest, parallel, alternative, etc.).
        // Example: (line breaks added for clarity)
        // ("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1") NIL NIL "7BIT" 2 1 NIL NIL NIL)
        // ("TEXT" "HTML" ("CHARSET" "ISO-8859-1") NIL NIL "7BIT" 27 1 NIL NIL NIL)
        // "ALTERNATIVE" ("BOUNDARY" "047d7bea2f06ddf77e04ec9b2a60") NIL NIL)
        // Or for non-multipart:
        // "TEXT" "PLAIN" ("CHARSET" "ISO-8859-1") NIL NIL "7BIT" 13 1 NIL NIL NIL
        public static MailMessage ParseBodyStructure(string rawBodyStructure, MailMessage messageData)
        {
            IList <string> tokens = ParseTokenList(rawBodyStructure);

            // Multipart or not?
            IList <string> subTokens = ParseTokenList(tokens[0]);

            if (subTokens.Count == 1)
            {
                // Singlepart
                messageData.ContentType             = GetContentType(tokens);
                messageData.ContentTransferEncoding = tokens[5];
                messageData.BodyId = "1";
                // TODO: Size
                // TODO: What are all the other fields?
            }
            else
            {
                messageData.Scope = Scope.HeadersAndMime;
                Attachment bodyPart;

                // Multipart
                int tokenIndex = 0;
                // Starts with a list of part descriptors:
                // "TEXT" "PLAIN" ("CHARSET" "ISO-8859-1") NIL NIL "7BIT" 13 1 NIL NIL NIL
                while (subTokens.Count > 1)
                {
                    int subtokenIndex = 0;
                    // Check for nested MIME, flatten it.
                    IList <string> subSubTokens = ParseTokenList(subTokens[subtokenIndex]);
                    if (subSubTokens.Count > 1)
                    {
                        do
                        {
                            bodyPart                         = new Attachment();
                            bodyPart.ContentType             = GetContentType(subSubTokens);
                            bodyPart.ContentTransferEncoding = subSubTokens[5];
                            bodyPart.BodyId                  = (tokenIndex + 1).ToString(CultureInfo.InvariantCulture)
                                                               + '.' + (subtokenIndex + 1).ToString(CultureInfo.InvariantCulture);
                            // TODO: Size
                            // TODO: What are all the other fields?
                            messageData.Add(bodyPart);

                            subtokenIndex++;
                            subSubTokens = ParseTokenList(subTokens[subtokenIndex]);
                        }while (subSubTokens.Count > 1);

                        // Ignore the content-type and boundary for nested mime.
                    }
                    else
                    {
                        bodyPart                         = new Attachment();
                        bodyPart.ContentType             = GetContentType(subTokens);
                        bodyPart.ContentTransferEncoding = subTokens[5];
                        bodyPart.BodyId                  = (tokenIndex + 1).ToString(CultureInfo.InvariantCulture);
                        // TODO: Size
                        // TODO: What are all the other fields?

                        messageData.Add(bodyPart);
                    }
                    tokenIndex++;
                    subTokens = ParseTokenList(tokens[tokenIndex]);
                }

                subTokens = ParseTokenList(tokens[tokenIndex + 1]);
                messageData.ContentType = "multipart/" + tokens[tokenIndex] + "; " + subTokens[0] + "=" + subTokens[1];
            }

            return(messageData);
        }