当前位置: 首页>>代码示例>>C#>>正文


C# Attachment.DecodeAsMessage方法代码示例

本文整理汇总了C#中Attachment.DecodeAsMessage方法的典型用法代码示例。如果您正苦于以下问题:C# Attachment.DecodeAsMessage方法的具体用法?C# Attachment.DecodeAsMessage怎么用?C# Attachment.DecodeAsMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Attachment的用法示例。


在下文中一共展示了Attachment.DecodeAsMessage方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParseMultipartMessageBody

        /// <summary>
        /// Parses the MessageBody as a Multipart message.
        /// This method will add these parts as Attachments
        /// </summary>
        private void ParseMultipartMessageBody()
        {
            string multipartBoundary = Headers.ContentType.Boundary;

            if (string.IsNullOrEmpty(multipartBoundary))
                throw new ArgumentException("The body is a multipart message, but there is no multipart boundary");

            int indexOfAttachmentStart = 0;
            bool moreParts = true;

            // Keep working until we have parsed every message part in this message
            while(moreParts)
            {
                // Find the start of the message parts multipartBoundary
                indexOfAttachmentStart = RawMessageBody.IndexOf(multipartBoundary, indexOfAttachmentStart);

                if(indexOfAttachmentStart == -1)
                    throw new ArgumentException("The start of the attachment could not be found");

                // Find the start of this message part - which does not include the multipartBoundary or the trailing CRLF
                indexOfAttachmentStart = indexOfAttachmentStart + multipartBoundary.Length + "\r\n".Length;

                // Find the end of the attachment, were we do not want the last line
                int indexOfAttachmentEnd = RawMessageBody.IndexOf(multipartBoundary, indexOfAttachmentStart);

                if(indexOfAttachmentEnd == -1)
                    throw new ArgumentException("The end of the attachment could not be found");

                // Check if this is the last part, which ends with the multipartBoundary followed by "--"
                if(RawMessageBody.Substring(indexOfAttachmentEnd).StartsWith(multipartBoundary + "--"))
                    moreParts = false;

                // Calculate the length. We do not want to include the last "\r\n" in the attachment
                int attachmentLength = indexOfAttachmentEnd - indexOfAttachmentStart - "\r\n".Length;

                string messagePart = RawMessageBody.Substring(indexOfAttachmentStart, attachmentLength);
                Attachment att = new Attachment(messagePart, Headers);

                // Check if this is the MS-TNEF attachment type
                // which has ContentType application/ms-tnef
                // and also if we should decode it
                if(MIMETypes.IsMSTNEF(att.Headers.ContentType.MediaType) && AutoDecodeMSTNEF)
                {
                    // It was a MS-TNEF attachment. Now we should parse it.
                    TNEFParser tnef = new TNEFParser(att.DecodedAsBytes());

                    if (tnef.Parse())
                    {
                        // ms-tnef attachment might contain multiple attachments inside it
                        foreach (TNEFAttachment tatt in tnef.Attachments())
                        {
                            Attachment attNew = new Attachment(tatt.FileContent, tatt.FileName, MIMETypes.GetMimeType(tatt.FileName));
                            Attachments.Add(attNew);
                        }
                    }
                    else
                        throw new ArgumentException("Could not parse TNEF attachment");
                }
                else if(IsMIMEMailFile2(att))
                {
                    // The attachment itself is a multipart message
                    // Parse it as such, and take the attachments from it
                    // and add it to our message
                    // This will in reality flatten the structure
                    Message m = att.DecodeAsMessage(true,true);
                    foreach (Attachment attachment in m.Attachments)
                        Attachments.Add(attachment);
                }
                else
                {
                    // This must be an attachment
                    Attachments.Add(att);
                }
            }
        }
开发者ID:aldrinmg,项目名称:SadPanda-1,代码行数:79,代码来源:Message.cs


注:本文中的Attachment.DecodeAsMessage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。