Пример #1
0
 public HeaderInfo ( System.Collections.Specialized.HybridDictionary headers ) {
     this.TopLevelMediaType = new MimeTopLevelMediaType();
     this.Enc = null;
     try {
         this.ContentType = MimeTools.parseHeaderFieldBody ( "Content-Type", headers["Content-Type"].ToString() );
         this.TopLevelMediaType = (MimeTopLevelMediaType)System.Enum.Parse(TopLevelMediaType.GetType(), this.ContentType["Content-Type"].Split('/')[0], true);
         this.Subtype = this.ContentType["Content-Type"].Split('/')[1];
         this.Enc = MimeTools.parseCharSet ( this.ContentType["charset"] );
     } catch (System.Exception) {
         this.Enc = default_encoding;
         this.ContentType = MimeTools.parseHeaderFieldBody ( "Content-Type", System.String.Concat("text/plain; charset=", this.Enc.BodyName) );
         this.TopLevelMediaType = MimeTopLevelMediaType.text;
         this.Subtype = "plain";
     }
     if ( this.Enc==null ) {
         this.Enc = default_encoding;
     }
     // TODO: rework this
     try {
         this.ContentdisPosition = MimeTools.parseHeaderFieldBody ( "Content-Disposition", headers["Content-Disposition"].ToString() );
     } catch ( System.Exception ) {
         this.ContentdisPosition = new System.Collections.Specialized.StringDictionary();
     }
     try {
         this.ContentLocation = MimeTools.parseHeaderFieldBody ( "Content-Location", headers["Content-Location"].ToString() );
     } catch ( System.Exception ) {
         this.ContentLocation = new System.Collections.Specialized.StringDictionary();
     }
 }
Пример #2
0
 public HeaderInfo(System.Collections.Specialized.HybridDictionary headers)
 {
     this.TopLevelMediaType = new MimeTopLevelMediaType();
     this.Enc = null;
     try {
         this.ContentType       = MimeTools.parseHeaderFieldBody("Content-Type", headers["Content-Type"].ToString());
         this.TopLevelMediaType = (MimeTopLevelMediaType)System.Enum.Parse(TopLevelMediaType.GetType(), this.ContentType["Content-Type"].Split('/')[0], true);
         this.Subtype           = this.ContentType["Content-Type"].Split('/')[1];
         this.Enc = MimeTools.parseCharSet(this.ContentType["charset"]);
     } catch (System.Exception) {
         this.Enc               = default_encoding;
         this.ContentType       = MimeTools.parseHeaderFieldBody("Content-Type", System.String.Concat("text/plain; charset=", this.Enc.BodyName));
         this.TopLevelMediaType = MimeTopLevelMediaType.text;
         this.Subtype           = "plain";
     }
     if (this.Enc == null)
     {
         this.Enc = default_encoding;
     }
     // TODO: rework this
     try {
         this.ContentdisPosition = MimeTools.parseHeaderFieldBody("Content-Disposition", headers["Content-Disposition"].ToString());
     } catch (System.Exception) {
         this.ContentdisPosition = new System.Collections.Specialized.StringDictionary();
     }
     try {
         this.ContentLocation = MimeTools.parseHeaderFieldBody("Content-Location", headers["Content-Location"].ToString());
     } catch (System.Exception) {
         this.ContentLocation = new System.Collections.Specialized.StringDictionary();
     }
 }
Пример #3
0
 public HeaderInfo(HybridDictionary headers)
 {
     TopLevelMediaType = new MimeTopLevelMediaType();
     enc = null;
     try
     {
         contenttype = SharpMimeTools.parseHeaderFieldBody("Content-Type", headers["Content-Type"].ToString());
         TopLevelMediaType = (MimeTopLevelMediaType)Enum.Parse(TopLevelMediaType.GetType(),
                                                        contenttype["Content-Type"].Split('/')[0].Trim(),
                                                        true);
         subtype = contenttype["Content-Type"].Split('/')[1].Trim();
         enc = SharpMimeTools.parseCharSet(contenttype["charset"]);
     }
     catch (Exception)
     {
         enc = SharpMimeHeader.default_encoding;
         contenttype = SharpMimeTools.parseHeaderFieldBody("Content-Type", String.Concat("text/plain; charset=", enc.BodyName));
         TopLevelMediaType = MimeTopLevelMediaType.text;
         subtype = "plain";
     }
     if (enc == null)
     {
         enc = SharpMimeHeader.default_encoding;
     }
     // TODO: rework this
     try
     {
         contentdisposition = SharpMimeTools.parseHeaderFieldBody("Content-Disposition", headers["Content-Disposition"].ToString());
     }
     catch (Exception)
     {
         contentdisposition = new StringDictionary();
     }
     try
     {
         contentlocation = SharpMimeTools.parseHeaderFieldBody("Content-Location", headers["Content-Location"].ToString());
     }
     catch (Exception)
     {
         contentlocation = new StringDictionary();
     }
 }
Пример #4
0
 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"];
     }
 }
Пример #5
0
        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;
            }
        }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SharpMessage" /> class based on the supplied <see cref="System.IO.Stream" />.
 /// </summary>
 /// <param name="message"><see cref="System.IO.Stream" /> that contains the message content</param>
 /// <param name="types">A <see cref="MimeTopLevelMediaType" /> value that specifies the allowed Mime-Types to being decoded.</param>
 /// <param name="options"><see cref="DecodeOptions" /> to determine how to do the decoding (must be combined as a bit map).</param>
 /// <param name="path">A <see cref="System.String" /> specifying the path on which to save the attachments found.</param>
 /// <param name="preferredtextsubtype">A <see cref="System.String" /> specifying the subtype to select for text parts that contain aternative content (plain, html, ...). Specify the <b>null</b> reference to maintain the default behavior (prefer whatever the originator sent as the preferred part). If there is not a text part with this subtype, the default one is used.</param>
 public SharpMessage(System.IO.Stream message, MimeTopLevelMediaType types, DecodeOptions options, System.String path, System.String preferredtextsubtype)
 {
     if (path != null && (options & DecodeOptions.CreateFolder) != DecodeOptions.CreateFolder && !System.IO.Directory.Exists(path))
     {
         path = null;
     }
     this.ParseMessage(message, types, options, preferredtextsubtype, path);
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SharpMessage" /> class based on the supplied <see cref="System.IO.Stream" />.
 /// </summary>
 /// <param name="message"><see cref="System.IO.Stream" /> that contains the message content</param>
 /// <param name="types">A <see cref="MimeTopLevelMediaType" /> value that specifies the allowed Mime-Types to being decoded.</param>
 /// <param name="html"><b>true</b> to allow HTML content; <b>false</b> to ignore the html content.</param>
 /// <param name="path">A <see cref="System.String" /> specifying the path on which to save the attachments found.</param>
 /// <param name="preferredtextsubtype">A <see cref="System.String" /> specifying the subtype to select for text parts that contain aternative content (plain, html, ...). Specify the <b>null</b> reference to maintain the default behavior (prefer whatever the originator sent as the preferred part). If there is not a text part with this subtype, the default one is used.</param>
 public SharpMessage(System.IO.Stream message, MimeTopLevelMediaType types, bool html, System.String path, System.String preferredtextsubtype)
     : this(message, types, ((html) ? DecodeOptions.Default : DecodeOptions.AllowAttachments), path, preferredtextsubtype)
 {
 }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SharpMessage" /> class based on the supplied <see cref="System.IO.Stream" />.
 /// </summary>
 /// <param name="message"><see cref="System.IO.Stream" /> that contains the message content</param>
 /// <param name="types">A <see cref="MimeTopLevelMediaType" /> value that specifies the allowed Mime-Types to being decoded.</param>
 /// <param name="html"><b>true</b> to allow HTML content; <b>false</b> to ignore the html content.</param>
 /// <param name="path">A <see cref="System.String" /> specifying the path on which to save the attachments found.</param>
 public SharpMessage(System.IO.Stream message, MimeTopLevelMediaType types, bool html, System.String path)
     : this(message, types, html, path, null)
 {
 }
Пример #9
0
 private void ParseMessage(Stream stream, MimeTopLevelMediaType types, SharpDecodeOptions options, String preferredtextsubtype, String path)
 {
     Attachments = new List<SharpAttachment>();
     using (SharpMimeMessage message = new SharpMimeMessage(stream))
     {
         ParseMessage(message, types, (options & SharpDecodeOptions.AllowHtml) == SharpDecodeOptions.AllowHtml, options, preferredtextsubtype, path);
         Headers = message.Header;
     }
     // find and decode uuencoded content if configured to do so (and attachments a allowed)
     if ((options & SharpDecodeOptions.UuDecode) == SharpDecodeOptions.UuDecode
            && (options & SharpDecodeOptions.AllowAttachments) == SharpDecodeOptions.AllowAttachments)
         UuDecode(path);
     // Date
     _date = SharpMimeTools.parseDate(Headers.Date);
     if (_date.Equals(DateTime.MinValue))
     {
         String date = Headers["Received"];
         if (date == null)
             date = String.Empty;
         if (date.IndexOf("\r\n") > 0)
             date = date.Substring(0, date.IndexOf("\r\n"));
         if (date.LastIndexOf(';') > 0)
             date = date.Substring(date.LastIndexOf(';') + 1).Trim();
         else
             date = String.Empty;
         _date = SharpMimeTools.parseDate(date);
     }
     // Subject
     _subject = SharpMimeTools.parserfc2047Header(Headers.Subject);
     // To
     To = SharpMimeAddressCollection.Parse(Headers.To);
     // From
     SharpMimeAddressCollection from = SharpMimeAddressCollection.Parse(Headers.From);
     foreach (SharpMimeAddress item in from)
     {
         _from_name = item.Name;
         _from_addr = item.Address;
         if (_from_name == null || _from_name.Equals(String.Empty))
             _from_name = item.Address;
     }
 }
Пример #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="anmar.SharpMimeTools.SharpMessage" /> class based on the supplied <see cref="System.IO.Stream" />.
 /// </summary>
 /// <param name="message"><see cref="System.IO.Stream" /> that contains the message content</param>
 /// <param name="types">A <see cref="anmar.SharpMimeTools.MimeTopLevelMediaType" /> value that specifies the allowed Mime-Types to being decoded.</param>
 /// <param name="html"><b>true</b> to allow HTML content; <b>false</b> to ignore the html content.</param>
 public SharpMessage(Stream message, MimeTopLevelMediaType types, bool html)
     : this(message, types, html, null, null)
 {
 }