示例#1
0
        protected override CompletionResponse Behaviour()
        {
            Exception     exception        = null;
            IPart         partByAttachment = this._message.GetPartByAttachment(this._attachmentDescription);
            uint          commandId        = base._dispatcher.SendCommand(string.Format("UID FETCH {0} (BODY.PEEK[{1}] BODY.PEEK[{1}.MIME])", this._message.Uid, this._message.GetPartIndex(partByAttachment)), base.filter);
            IMAP4Response response         = base._dispatcher.GetResponse(commandId);

            while (!response.IsCompletionResponse())
            {
                if (response.Name != "FETCH")
                {
                    throw new UnexpectedResponseException("Unexpected response");
                }
                ulong             size   = base.GetSize(response);
                FileContentWriter writer = new FileContentWriter(this._attachmentDirectory);
                try
                {
                    writer.Open();
                    string str = string.Empty;
                    while (size > 0L)
                    {
                        ulong  num3  = (size > 0x1000L) ? ((ulong)0x1000L) : size;
                        byte[] bytes = base._dispatcher.GetRawData(num3);
                        byte[] data  = bytes;
                        if (partByAttachment.Header.ContentTransferEncoding == EContentTransferEncoding.Base64)
                        {
                            string str2 = str + Encoding.UTF8.GetString(bytes, 0, bytes.Length).Replace("\r\n", "");
                            data = Convert.FromBase64String(str2.Substring(0, str2.Length - (str2.Length % 4)));
                            str  = str2.Substring(str2.Length - (str2.Length % 4));
                        }
                        writer.Write(data);
                        size -= num3;
                    }
                }
                catch (Exception exception2)
                {
                    exception = exception2;
                    base._dispatcher.GetRawData(size);
                }
                finally
                {
                    writer.Close();
                }
                this._attachmentDescription.DiskFilename = writer.Filename;
                byte[] rawData = base._dispatcher.GetRawData();
                string s       = Encoding.UTF8.GetString(rawData, 0, rawData.Length) + "\r\n";
                rawData = Encoding.UTF8.GetBytes(s);
                Match match = new Regex("{(?<size>[0-9]+)}|(?<size>NIL)").Match(s);
                if (match.Success && (match.Groups["size"].Value != "NIL"))
                {
                    size = ulong.Parse(match.Groups["size"].Value);
                    byte[] sourceArray      = base._dispatcher.GetRawData(size);
                    byte[] destinationArray = new byte[rawData.Length + sourceArray.Length];
                    Array.Copy(rawData, destinationArray, rawData.Length);
                    Array.Copy(sourceArray, 0, destinationArray, rawData.Length, sourceArray.Length);
                    this._message = new MessageDescriptionParser().Parse(this._message, destinationArray, this._message.Uid, this._attachmentDirectory);
                    base._dispatcher.GetRawData();
                }
                response = base._dispatcher.GetResponse(commandId);
            }
            if (exception != null)
            {
                throw exception;
            }
            return(new CompletionResponse(response.Response));
        }
 public ContentPartParser(string attachmentDirectory)
 {
     this._contentWriter = new FileContentWriter(attachmentDirectory);
 }