private void ParseMessage(System.IO.Stream stream, MimeTopLevelMediaType types, DecodeOptions options, System.String preferredtextsubtype, System.String path) { this._attachments = new System.Collections.ArrayList(); MimeMessage message = new MimeMessage(stream); this.ParseMessage(message, types, (options & DecodeOptions.AllowHtml) == DecodeOptions.AllowHtml, options, preferredtextsubtype, path); this._headers = message.Header; message.Close(); // find and decode uuencoded content if configured to do so (and attachments a allowed) if ((options & DecodeOptions.UuDecode) == DecodeOptions.UuDecode && (options & DecodeOptions.AllowAttachments) == DecodeOptions.AllowAttachments) this.UuDecode(path); // Date this._date = MimeTools.parseDate(this._headers.Date); if (this._date.Equals(System.DateTime.MinValue)) { System.String date = this._headers["Received"] ?? System.String.Empty; if (date.IndexOf("\r\n", System.StringComparison.Ordinal) > 0) date = date.Substring(0, date.IndexOf("\r\n", System.StringComparison.Ordinal)); date = date.LastIndexOf(';') > 0 ? date.Substring(date.LastIndexOf(';') + 1).Trim() : System.String.Empty; this._date = MimeTools.parseDate(date); } // Subject this._subject = MimeTools.parserfc2047Header(this._headers.Subject); // To this._to = MimeAddressCollection.Parse(this._headers.To); // From MimeAddressCollection from = MimeAddressCollection.Parse(this._headers.From); foreach (MimeAddress item in from) { this._from_name = item["name"]; this._from_addr = item["address"]; if (this._from_name == null || this._from_name.Equals(System.String.Empty)) this._from_name = item["address"]; } }
private void ParseMessage(MimeMessage part, MimeTopLevelMediaType types, bool html, DecodeOptions options, System.String preferredtextsubtype, System.String path) { if ((types & part.Header.TopLevelMediaType) != part.Header.TopLevelMediaType) { return; } switch (part.Header.TopLevelMediaType) { case MimeTopLevelMediaType.multipart: case MimeTopLevelMediaType.message: // TODO: allow other subtypes of "message" // Only message/rfc822 allowed, other subtypes ignored if (part.Header.TopLevelMediaType.Equals(MimeTopLevelMediaType.message) && !part.Header.SubType.Equals("rfc822")) break; if (part.Header.SubType.Equals("alternative")) { if (part.PartsCount > 0) { MimeMessage altenative = null; // Get the first mime part of the alternatives that has a accepted Mime-Type for (int i = part.PartsCount; i > 0; i--) { MimeMessage item = part.GetPart(i - 1); if ((types & part.Header.TopLevelMediaType) != part.Header.TopLevelMediaType || (!html && item.Header.TopLevelMediaType.Equals(MimeTopLevelMediaType.text) && item.Header.SubType.Equals("html")) ) { continue; } // First allowed one. if (altenative == null) { altenative = item; // We don't have to select body part based on subtype if not asked for, or not a text one // or it's already the preferred one if (preferredtextsubtype == null || item.Header.TopLevelMediaType != MimeTopLevelMediaType.text || (item.Header.SubType == preferredtextsubtype)) { break; } // This one is preferred over the last part } else if (item.Header.TopLevelMediaType == MimeTopLevelMediaType.text && item.Header.SubType == preferredtextsubtype) { altenative = item; break; } } if (altenative != null) { // If message body as html is allowed and part has a Content-ID field // add an anchor to mark this body part if (html && part.Header.Contains("Content-ID") && (options & DecodeOptions.NamedAnchors) == DecodeOptions.NamedAnchors) { // There is a previous text body, so enclose it in <pre> if (!this._bodyHtml && this._body.Length > 0) { this._body = System.String.Concat("<pre>", System.Web.HttpUtility.HtmlEncode(this._body), "</pre>"); this._bodyHtml = true; } // Add the anchor this._body = System.String.Concat(this._body, "<a name=\"", MimeTools.Rfc2392Url(this.MessageID), "_", MimeTools.Rfc2392Url(part.Header.ContentID), "\"></a>"); } this.ParseMessage(altenative, types, html, options, preferredtextsubtype, path); } } // TODO: Take into account each subtype of "multipart" } else if (part.PartsCount > 0) { foreach (MimeMessage item in part) { this.ParseMessage(item, types, html, options, preferredtextsubtype, path); } } break; case MimeTopLevelMediaType.text: if ((part.Disposition == null || !part.Disposition.Equals("attachment")) && (part.Header.SubType.Equals("plain") || part.Header.SubType.Equals("html"))) { bool body_was_html = this._bodyHtml; // HTML content not allowed if (part.Header.SubType.Equals("html")) { if (!html) break; this._bodyHtml = true; } if (html && part.Header.Contains("Content-ID") && (options & DecodeOptions.NamedAnchors) == DecodeOptions.NamedAnchors) { this._bodyHtml = true; } if (this._bodyHtml && !body_was_html && this._body.Length > 0) { this._body = System.String.Concat("<pre>", System.Web.HttpUtility.HtmlEncode(this._body), "</pre>"); } // If message body is html and this part has a Content-ID field // add an anchor to mark this body part if (this._bodyHtml && part.Header.Contains("Content-ID") && (options & DecodeOptions.NamedAnchors) == DecodeOptions.NamedAnchors) { this._body = System.String.Concat(this._body, "<a name=\"", MimeTools.Rfc2392Url(this.MessageID), "_", MimeTools.Rfc2392Url(part.Header.ContentID), "\"></a>"); } if (this._bodyHtml && part.Header.SubType.Equals("plain")) { this._body = System.String.Concat(this._body, "<pre>", System.Web.HttpUtility.HtmlEncode(part.BodyDecoded), "</pre>"); } else this._body = System.String.Concat(this._body, part.BodyDecoded); } else { if ((types & MimeTopLevelMediaType.application) != MimeTopLevelMediaType.application) { return; } goto case MimeTopLevelMediaType.application; } break; case MimeTopLevelMediaType.application: case MimeTopLevelMediaType.audio: case MimeTopLevelMediaType.image: case MimeTopLevelMediaType.video: // Attachments not allowed. if ((options & DecodeOptions.AllowAttachments) != DecodeOptions.AllowAttachments) break; SharpAttachment attachment = null; // Save to a file if (path != null) { System.IO.FileInfo file = part.DumpBody(path, true); if (file != null) { attachment = new SharpAttachment(file); attachment.Name = file.Name; attachment.Size = file.Length; } // Save to a stream } else { System.IO.MemoryStream stream = new System.IO.MemoryStream(); part.DumpBody(stream); attachment = new SharpAttachment(stream); attachment.Name = part.Name ?? System.String.Concat("generated_", part.GetHashCode(), ".", part.Header.SubType); attachment.Size = stream.Length; } if (attachment != null && part.Header.SubType == "ms-tnef" && (options & DecodeOptions.DecodeTnef) == DecodeOptions.DecodeTnef) { // Try getting attachments form a tnef stream System.IO.Stream stream = attachment.Stream; if (stream != null && stream.CanSeek) stream.Seek(0, System.IO.SeekOrigin.Begin); TnefMessage tnef = new TnefMessage(stream); if (tnef.Parse(path)) { if (tnef.Attachments != null) { this._attachments.AddRange(tnef.Attachments); } attachment.Close(); // Delete the raw tnef file if (attachment.SavedFile != null) { if (stream != null && stream.CanRead) stream.Close(); attachment.SavedFile.Delete(); } attachment = null; tnef.Close(); } else { // The read-only stream is no longer needed and locks the file if (attachment.SavedFile != null && stream != null && stream.CanRead) stream.Close(); } } if (attachment != null) { if (part.Disposition != null && part.Disposition == "inline") { attachment.Inline = true; } attachment.MimeTopLevelMediaType = part.Header.TopLevelMediaType; attachment.MimeMediaSubType = part.Header.SubType; // Store attachment's CreationTime if (part.Header.ContentDispositionParameters.ContainsKey("creation-date")) attachment.CreationTime = MimeTools.parseDate(part.Header.ContentDispositionParameters["creation-date"]); // Store attachment's LastWriteTime if (part.Header.ContentDispositionParameters.ContainsKey("modification-date")) attachment.LastWriteTime = MimeTools.parseDate(part.Header.ContentDispositionParameters["modification-date"]); if (part.Header.Contains("Content-ID")) attachment.ContentID = part.Header.ContentID; this._attachments.Add(attachment); } break; } }
private void Parse () { if ( !this.IsMultipart || this.Equals(this._mi.parts.Parent) ) { return; } switch (this._mi.header.TopLevelMediaType) { case MimeTopLevelMediaType.message: this._mi.parts.Parent = this; MimeMessage mimeMessage = new MimeMessage (this.message, this._mi.start_body, this._mi.end ); this._mi.parts.Add (mimeMessage); break; case MimeTopLevelMediaType.multipart: this.message.SeekPoint ( this._mi.start_body ); System.String line; this._mi.parts.Parent = this; System.String boundary_start = System.String.Concat("--", this._mi.header.ContentTypeParameters["boundary"]); System.String boundary_end = System.String.Concat("--", this._mi.header.ContentTypeParameters["boundary"], "--"); for ( line=this.message.ReadLine(); line!=null ; line=this.message.ReadLine() ) { // It can't be a boundary line if ( line.Length<3 ) continue; // Match start boundary line if ( line.Length==boundary_start.Length && line==boundary_start ) { if ( this._mi.parts.Count>0 ) { this._mi.parts.Get( this._mi.parts.Count-1 )._mi.end = this.message.Position_preRead; } MimeMessage msg = new MimeMessage (this.message, this.message.Position ); this._mi.parts.Add (msg); // Match end boundary line } else if ( line.Length==boundary_end.Length && line==boundary_end ) { this._mi.end = this.message.Position_preRead; if ( this._mi.parts.Count>0 ) { this._mi.parts.Get( this._mi.parts.Count-1 )._mi.end = this.message.Position_preRead; } break; } } break; } return; }