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 MailProperties ReadHeader(MailReader reader) { string temp; string reply = reader.ReadLine(); ResetTimer(); if ((reply == null) || reply.Equals(".")) { return(null); } MailProperties properties = null; while (!string.IsNullOrEmpty(reply)) { if (properties == null) { properties = new MailProperties(); } temp = reader.ReadLine(); ResetTimer(); if (temp == null) { return(properties); } if (temp.Equals(".")) { return(properties); } bool returnProps = false; // MultiLine headers have a space\tab at begining of each extra line while ((temp.StartsWith(" ")) || (temp.StartsWith("\t"))) { reply = reply + CRLF + temp; temp = reader.ReadLine(); ResetTimer(); if ((temp == null) || temp.Equals(".")) { returnProps = true; break; } } properties.PutKey(reply); reply = temp; if (returnProps) { return(properties); } } return(properties); }
private static string GetAttachmentContentId(MailProperties partProps) { string cid = partProps.GetKeyPrincipal("Content-ID"); if (!string.IsNullOrEmpty(cid) && cid.StartsWith("<") && cid.EndsWith(">")) { cid = cid.Substring(1, cid.Length - 2); } return(cid); }
private void ReadMessage(MailReader reader, MailProperties partProps, string separator) { reader.SetEncoding(partProps.GetCharset()); string mimeMediaType = partProps.GetMimeMediaType(); if (string.Compare(mimeMediaType, "multipart", true) == 0) { ReadMultipartBody(reader, partProps); } else { ReadBody(reader, partProps, separator); } }
private bool AttachmentContentDisposition(MailProperties partProps) { string contentDisposition = partProps.GetKeyPrincipal("Content-Disposition"); if (string.Compare(contentDisposition, "attachment", true) == 0) { return(true); } string transferEncoding = partProps.GetKeyPrincipal("Content-Transfer-Encoding"); if ((string.Compare(contentDisposition, "inline", true) == 0) && (string.Compare(transferEncoding, "base64", true) == 0)) { return(true); } return(false); }
private static string GetSeparator(MailProperties properties) { string separator = properties.GetKeyProperty("Content-Type", "boundary"); if (separator.Length == 0) { return(null); } if (separator[0] == '"') { separator = separator.Substring(1, separator.Length - 2); } return("--" + separator); }
public void ReadEntireMessage() { try { keys = ReadHeader(reader); ReadMessage(reader, keys, null); } catch (Exception e) { GXLogging.Error(log, "ReadEntireMessage error", e); throw e; } finally { try { reader.ReadToEnd(); } catch {} reader.ResetState(); } }
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); }