/// <summary> Parse content disposition.</summary> /// <param name="header"> The header. </param> private void ParseContentDisposition(MIMEheader header) { //From = header.Value.Trim(); var semSplit = new Regex("(?:^|;)(\"(?:[^\"]+|\"\")*\"|[^;]*)", RegexOptions.Compiled); var first = true; foreach (Match match in semSplit.Matches(header.Value)) { if (first) { this.ContentDisposition = match.Value.Trim(); first = false; } else { var trimPart = match.Value.TrimStart(';').Trim(); var equalSeparator = new[] { '=' }; var partParts = trimPart.Split(equalSeparator, 2); if (partParts.Count() == 2) { partParts[1] = partParts[1].Trim(' ', '\t', '\n', '\v', '\f', '\r', '"'); if (partParts[0].Equals("filename")) { this.ContentDispositionFileName = partParts[1]; } } } } }
/// <summary> Parse content transfer encoding.</summary> /// <param name="header"> The header. </param> private void ParseContentTransferEncoding(MIMEheader header) { switch (header.Value.Trim()) { case "7bit": this.ContentTransferEncoding = ContentTransferEncodingType.Bit7; break; case "8bit": this.ContentTransferEncoding = ContentTransferEncodingType.Bit8; break; case "binary": this.ContentTransferEncoding = ContentTransferEncodingType.Binary; break; case "quoted-printable": this.ContentTransferEncoding = ContentTransferEncodingType.Quotedprintable; break; case "base64": this.ContentTransferEncoding = ContentTransferEncodingType.Base64; break; case "ietf-token": this.ContentTransferEncoding = ContentTransferEncodingType.Ietftoken; break; case "x-token": this.ContentTransferEncoding = ContentTransferEncodingType.Xtoken; break; } }
/// <summary> Parse date.</summary> /// <param name="header"> The header. </param> private void ParseDate(MIMEheader header) { var dateString = header.Value.Trim(); var parts = dateString.Split(' '); if (parts.Count() > 6) { var partsList = new List <string>(parts); partsList.RemoveRange(6, partsList.Count - 6); dateString = string.Join(" ", partsList); } var myCultureInfo = new CultureInfo("en-US"); try { var partDateTime = DateTime.ParseExact(dateString, "ddd, dd MMM yyyy HH:mm:ss zzzz", CultureInfo.InvariantCulture); this.Date = partDateTime.ToUniversalTime().ToString("yyyy-MM-dd hh.mm.ss.fffffff"); } catch (FormatException ex) { // _log.Error("Sleuth FormatException", ex); this._email.AddErrorReport(FcLogLevel.Warn, "MIME Part", "Unable to parse date", dateString, ex); //Console.WriteLine("Unable to parse '{0}' '{1}'", dateString,ex.Message); } }
/// <summary> Parse content identifier.</summary> /// <param name="header"> The header. </param> private void ParseContentId(MIMEheader header) { var contentID = header.Value.Trim(); /* contentID = contentID.Remove(0, 1); * contentID = contentID.Remove(contentID.Length-1, 1); */ this.ContentID = contentID; }
/// <summary> Parse headers.</summary> /// <param name="dataStream"> The data stream. </param> /// <param name="currentPart"> The current part. </param> /// <param name="parentPart"> The parent part. </param> /// <param name="encoding"> The encoding. </param> /// <param name="boundaryFound"> [out] The boundary found. </param> public void ParseHeaders(LineReader dataStream, MIMEpart currentPart, MIMEpart parentPart, Encoding encoding, out bool boundaryFound) { var separator = new char[] { ':' }; var line = String.Empty; MIMEheader currentHeader = null; boundaryFound = false; while (line != null) { // read line by line //line = dataStream.ReadLine();) line = dataStream.ReadLine(encoding); if (line != null) { // break whene empty line (headers delimiter was found) // or end of document was reached if (line.Equals(String.Empty) || line.Length == 0) { break; } // check for parrent boundary line - in case of empty part conent if (parentPart != null && parentPart.ContentBoundary != null) { if (line.Equals(parentPart.ContentBoundary) || line.Equals(parentPart.PartBoundary)) { // content is empty - break boundaryFound = true; break; } } // store line to raw headers string currentPart.RawHeaders += line + Environment.NewLine; // handle multiline headers : if (char.IsWhiteSpace(line[0])) { // if header line begins with white character // add its conent to previous header value if (currentHeader != null) { currentHeader.Value += line.Trim(); } } else { // next header was detected : if (currentHeader != null) { // if this is not the first one - store previous header currentPart.AddHeader(currentHeader); currentHeader = null; } // split first part of header to type and value parts : var parts = line.Split(separator, 2); if (parts.Count() == 2) { currentHeader = new MIMEheader(); currentHeader.Type = parts[0].Trim(); currentHeader.Value = parts[1].Trim(); } } } } // store last found header before exit if (currentHeader != null) { currentPart.AddHeader(currentHeader); } }
/// <summary> Parse content type.</summary> /// <param name="header"> The header. </param> private void ParseContentType(MIMEheader header) { var semSplit = new Regex("(?:^|;)(\"(?:[^\"]+|\"\")*\"|[^;]*)", RegexOptions.Compiled); var first = true; foreach (Match match in semSplit.Matches(header.Value)) { if (first) { var slashSeparator = new[] { '/' }; var typeParts = match.Value.Split(slashSeparator); if (typeParts.Count() != 2) { return; } this.ContentType = typeParts[0].Trim(); this.ContentSubtype = typeParts[1].Trim(); if (this.ContentType.Equals("multipart")) { this.IsMultiPart = true; } first = false; } else { var trimPart = match.Value.TrimStart(';').Trim(); var equalSeparator = new[] { '=' }; var partParts = trimPart.Split(equalSeparator, 2); if (partParts.Count() == 2) { partParts[1] = partParts[1].Trim(' ', '\t', '\n', '\v', '\f', '\r', '"'); if (partParts[0].Equals("boundary")) { this._boundary = partParts[1]; } else if (partParts[0].Equals("name")) { this.ContentTypeName = partParts[1]; } else if (partParts[0].Equals("charset")) { this.ContentTypeCharset = partParts[1]; try { this.Encoding = Encoding.GetEncoding(this.ContentTypeCharset); ; } catch (Exception ex) { this._email.AddErrorReport(FcLogLevel.Warn, "MIME Part", "Unable to determine ConentType-Chartset", this.ContentTypeCharset, ex); } } } } } }
/// <summary> Parse in reply to.</summary> /// <param name="header"> The header. </param> private void ParseInReplyTo(MIMEheader header) { this.InReplyTo = header.Value.Trim(); }
/// <summary> Parse message identifier.</summary> /// <param name="header"> The header. </param> private void ParseMessageID(MIMEheader header) { var messageID = header.Value.Trim(); this.MessageID = messageID; }
/// <summary> Parse Bcc.</summary> /// <param name="header"> The header. </param> private void ParseBcc(MIMEheader header) { this.Bcc = header.Value.Trim(); }
/// <summary> Parse Cc.</summary> /// <param name="header"> The header. </param> private void ParseCc(MIMEheader header) { this.Cc = header.Value.Trim(); }
/// <summary> Parse to.</summary> /// <param name="header"> The header. </param> private void ParseTo(MIMEheader header) { this.To = header.Value.Trim(); }
/// <summary> Parse subject.</summary> /// <param name="header"> The header. </param> private void ParseSubject(MIMEheader header) { this.Subject = header.Value.Trim(); }
/// <summary> Parse from.</summary> /// <param name="header"> The header. </param> private void ParseFrom(MIMEheader header) { this.From = header.Value.Trim(); }
/// <summary> /// Add preparsed header to the part. Important headers are analyzed and mirrored in part /// properties. /// </summary> /// <param name="header"> The header. </param> public void AddHeader(MIMEheader header) { if (header == null) { return; } this._headers.Add(header); if (header.Type.Equals("MIME-Version") && header.Value.Trim().Equals("1.0")) { this.IsMIMEpart = true; } else if (header.Type.Equals("Content-Type")) { this.ParseContentType(header); } else if (header.Type.Equals("Content-ID")) { this.ParseContentId(header); } else if (header.Type.Equals("Content-Transfer-Encoding")) { this.ParseContentTransferEncoding(header); } else if (header.Type.Equals("Message-ID")) { this.ParseMessageID(header); } else if (header.Type.Equals("In-Reply-To")) { this.ParseInReplyTo(header); } else if (header.Type.Equals("From")) { this.ParseFrom(header); } else if (header.Type.Equals("To")) { this.ParseTo(header); } else if (header.Type.Equals("Date")) { this.ParseDate(header); } else if (header.Type.Equals("Content-Disposition")) { this.ParseContentDisposition(header); } else if (header.Type.Equals("Cc")) { this.ParseCc(header); } else if (header.Type.Equals("Bcc")) { this.ParseBcc(header); } else if (header.Type.Equals("Subject")) { this.ParseSubject(header); } }