private void ReadMultipartBody(MailReader reader, MailProperties properties) { string separator = GetSeparator(properties); string subtype = properties.GetMimeMediaSubtype(); reader.SetSeparator(null); string reply = reader.ReadLine(); ResetTimer(); if (reply != null) { while (reply != null && !reply.StartsWith(separator)) { reply = reader.ReadLine(); ResetTimer(); } } reader.SetSeparator(separator); while (true) { MailProperties props = ReadHeader(reader); if (props == null) { break; } ReadMessage(reader, props, separator); } }
private void ReadBody(MailReader reader, MailProperties partProps, String separator) { string contentType = partProps.GetKeyPrincipal("Content-Type"); bool isTextPlain = (string.Compare(contentType, "text/plain", true) == 0); bool isTextHtml = (string.Compare(contentType, "text/html", true) == 0); bool isAttachment = (!isTextPlain && !isTextHtml) || AttachmentContentDisposition(partProps); string oldSeparator = reader.GetSeparator(); reader.SetSeparator(separator); reader.SetTextPlain(isTextPlain); GXLogging.Debug(log, "isAttachment:" + isAttachment + " isTextHTML:" + isTextHtml + " istextplain:" + isTextPlain); MimeDecoder decoder = GetDecoder(partProps.GetField("Content-Transfer-Encoding")); if (isAttachment) { string outname = string.Empty; try { Stream output; if (this.DownloadAttachments) { string cid = GetAttachmentContentId(partProps); string fileName = qpDecoder.DecodeHeader(RemoveQuotes(partProps.GetKeyProperty("Content-Disposition", "filename"))); if (string.IsNullOrEmpty(fileName)) { fileName = RemoveQuotes(partProps.GetKeyProperty("Content-Type", "name")); } bool inlineAttachment = partProps.GetKeyPrincipal("Content-Disposition").StartsWith("inline"); if (inlineAttachment && !string.IsNullOrEmpty(cid)) { fileName = String.Format("{0}_{1}", cid, fileName); outname = GetFileName(attachmentsPath, fileName, contentType); this.messageHtml = this.messageHtml.Replace("cid:" + cid, outname); } else { outname = GetFileName(attachmentsPath, fileName, contentType); } attachments += outname + ";"; if (!Directory.Exists(attachmentsPath)) { Directory.CreateDirectory(attachmentsPath); } output = new FileStream(attachmentsPath + outname, FileMode.Create); } else { output = new DummyStream(); } try { decoder.DecodeFile(reader, output, runner); } catch (Exception e) { GXLogging.Error(log, "ReadBody error", e); throw e; } finally { output.Close(); } } catch (DirectoryNotFoundException dex) { GXLogging.Error(log, "Error with attachments, attachment path:" + attachmentsPath + outname, dex); throw new CouldNotSaveAttachmentException(dex); } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } GXLogging.Error(log, "Error with attachments, attachment path:" + attachmentsPath + outname, ex); throw new InvalidAttachmentException(ex); } } else { try { TextWriter output = new StringWriter(); try { decoder.DecodeText(reader, output, runner); if (isTextPlain) { messageText.Append(((StringWriter)output).GetStringBuilder().ToString()); } else if (isTextHtml) { messageHtml.Append(((StringWriter)output).GetStringBuilder().ToString()); } } catch (Exception e) { GXLogging.Error(log, "ReadBody error", e); throw e; } finally { output.Close(); } } catch (Exception ex1) { if (ex1.InnerException != null) { ex1 = ex1.InnerException; } GXLogging.Error(log, "ReadBody error", ex1); throw new InvalidMessageException(ex1); } } reader.SetSeparator(oldSeparator); }