Example #1
0
 private void ParseMessage( anmar.SharpMimeTools.SharpMimeMessage part, anmar.SharpMimeTools.MimeTopLevelMediaType types, bool html, SharpDecodeOptions options, System.String preferredtextsubtype, System.String path )
 {
     if ( (types&part.Header.TopLevelMediaType)!=part.Header.TopLevelMediaType ) {
     #if LOG
         if ( log.IsDebugEnabled )
             log.Debug (System.String.Concat("Mime-Type [", part.Header.TopLevelMediaType, "] is not an accepted Mime-Type. Skiping part."));
     #endif
         return;
     }
     switch ( part.Header.TopLevelMediaType ) {
         case anmar.SharpMimeTools.MimeTopLevelMediaType.multipart:
         case anmar.SharpMimeTools.MimeTopLevelMediaType.message:
             // TODO: allow other subtypes of "message"
             // Only message/rfc822 allowed, other subtypes ignored
             if ( part.Header.TopLevelMediaType.Equals(anmar.SharpMimeTools.MimeTopLevelMediaType.message)
                  && !part.Header.SubType.Equals("rfc822") )
                 break;
             if ( part.Header.SubType.Equals ("alternative") ) {
                 if ( part.PartsCount>0 ) {
                     anmar.SharpMimeTools.SharpMimeMessage altenative = null;
                     // Get the first mime part of the alternatives that has a accepted Mime-Type
                     for ( int i=part.PartsCount; i>0; i-- ) {
                         anmar.SharpMimeTools.SharpMimeMessage item = part.GetPart(i-1);
                         if ( (types&part.Header.TopLevelMediaType)!=part.Header.TopLevelMediaType
                             || ( !html && item.Header.TopLevelMediaType.Equals(anmar.SharpMimeTools.MimeTopLevelMediaType.text) && item.Header.SubType.Equals("html") )
                            ) {
     #if LOG
                            	if ( log.IsDebugEnabled )
                            		log.Debug (System.String.Concat("Mime-Type [", item.Header.TopLevelMediaType, "/", item.Header.SubType, "] is not an accepted Mime-Type. Skiping alternative part."));
     #endif
                             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!=anmar.SharpMimeTools.MimeTopLevelMediaType.text || (preferredtextsubtype!=null && item.Header.SubType==preferredtextsubtype) ) {
                                 break;
                             }
                         // This one is preferred over the last part
                         } else if ( preferredtextsubtype!=null && item.Header.TopLevelMediaType==anmar.SharpMimeTools.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&anmar.SharpMimeTools.SharpDecodeOptions.NamedAnchors)==anmar.SharpMimeTools.SharpDecodeOptions.NamedAnchors ) {
                             // There is a previous text body, so enclose it in <pre>
                             if ( !this._body_html && this._body.Length>0 ) {
                                 this._body = System.String.Concat ("<pre>", System.Web.HttpUtility.HtmlEncode(this._body), "</pre>");
                                 this._body_html = true;
                             }
                             // Add the anchor
                             this._body = System.String.Concat (this._body, "<a name=\"", anmar.SharpMimeTools.SharpMimeTools.Rfc2392Url(this.MessageID), "_", anmar.SharpMimeTools.SharpMimeTools.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 ( anmar.SharpMimeTools.SharpMimeMessage item in part ) {
                     this.ParseMessage(item, types, html, options, preferredtextsubtype, path);
                 }
             }
             break;
         case anmar.SharpMimeTools.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._body_html;
                 // HTML content not allowed
                 if ( part.Header.SubType.Equals("html") ) {
                     if ( !html )
                         break;
                     else
                         this._body_html=true;
                 }
                 if ( html && part.Header.Contains("Content-ID") && (options&anmar.SharpMimeTools.SharpDecodeOptions.NamedAnchors)==anmar.SharpMimeTools.SharpDecodeOptions.NamedAnchors ) {
                     this._body_html = true;
                 }
                 if ( this._body_html && !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._body_html && part.Header.Contains("Content-ID") && (options&anmar.SharpMimeTools.SharpDecodeOptions.NamedAnchors)==anmar.SharpMimeTools.SharpDecodeOptions.NamedAnchors ) {
                     this._body = System.String.Concat (this._body, "<a name=\"", anmar.SharpMimeTools.SharpMimeTools.Rfc2392Url(this.MessageID), "_", anmar.SharpMimeTools.SharpMimeTools.Rfc2392Url(part.Header.ContentID), "\"></a>");
                 }
                 if ( this._body_html && 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&anmar.SharpMimeTools.MimeTopLevelMediaType.application)!=anmar.SharpMimeTools.MimeTopLevelMediaType.application ) {
     #if LOG
                     if ( log.IsDebugEnabled )
                         log.Debug (System.String.Concat("Mime-Type [", anmar.SharpMimeTools.MimeTopLevelMediaType.application, "] is not an accepted Mime-Type. Skiping part."));
     #endif
                     return;
                 }
                 goto case anmar.SharpMimeTools.MimeTopLevelMediaType.application;
             }
             break;
         case anmar.SharpMimeTools.MimeTopLevelMediaType.application:
         case anmar.SharpMimeTools.MimeTopLevelMediaType.audio:
         case anmar.SharpMimeTools.MimeTopLevelMediaType.image:
         case anmar.SharpMimeTools.MimeTopLevelMediaType.video:
             // Attachments not allowed.
             if ( (options&anmar.SharpMimeTools.SharpDecodeOptions.AllowAttachments)!=anmar.SharpMimeTools.SharpDecodeOptions.AllowAttachments )
                 break;
             anmar.SharpMimeTools.SharpAttachment attachment = null;
             // Save to a file
             if ( path!=null ) {
                 System.IO.FileInfo file = part.DumpBody(path, true);
                 if ( file!=null ) {
                     attachment = new anmar.SharpMimeTools.SharpAttachment(file);
                     attachment.Name = file.Name;
                     attachment.Size = file.Length;
                 }
             // Save to a stream
             } else {
                 System.IO.MemoryStream stream = new System.IO.MemoryStream();
                 if ( part.DumpBody(stream) ) {
                     attachment = new anmar.SharpMimeTools.SharpAttachment(stream);
                     if ( part.Name!=null )
                         attachment.Name = part.Name;
                     else
                         attachment.Name = System.String.Concat("generated_", part.GetHashCode(), ".", part.Header.SubType);
                     attachment.Size = stream.Length;
                 }
                 stream = null;
             }
             if ( attachment!=null && part.Header.SubType=="ms-tnef" && (options&anmar.SharpMimeTools.SharpDecodeOptions.DecodeTnef)==anmar.SharpMimeTools.SharpDecodeOptions.DecodeTnef ) {
             // Try getting attachments form a tnef stream
     #if LOG
                 if ( log.IsDebugEnabled ) {
                     log.Debug(System.String.Concat("Decoding ms-tnef stream."));
                 }
     #endif
                 System.IO.Stream stream = attachment.Stream;
                 if ( stream!=null && stream.CanSeek )
                     stream.Seek(0, System.IO.SeekOrigin.Begin);
                 anmar.SharpMimeTools.SharpTnefMessage tnef = new anmar.SharpMimeTools.SharpTnefMessage(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();
     #if LOG
                     if ( log.IsDebugEnabled ) {
                         log.Debug(System.String.Concat("ms-tnef stream decoded successfully. Found [", ((tnef.Attachments!=null)?tnef.Attachments.Count:0),"] attachments."));
                     }
     #endif
                 } else {
                     // The read-only stream is no longer needed and locks the file
                     if ( attachment.SavedFile!=null && stream!=null && stream.CanRead )
                         stream.Close();
                 }
                 stream = null;
                 tnef = null;
             }
             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 = anmar.SharpMimeTools.SharpMimeTools.parseDate ( part.Header.ContentDispositionParameters["creation-date"] );
                 // Store attachment's LastWriteTime
                 if ( part.Header.ContentDispositionParameters.ContainsKey("modification-date") )
                     attachment.LastWriteTime = anmar.SharpMimeTools.SharpMimeTools.parseDate ( part.Header.ContentDispositionParameters["modification-date"] );
                 if ( part.Header.Contains("Content-ID") )
                     attachment.ContentID = part.Header.ContentID;
                 this._attachments.Add(attachment);
             }
             break;
         default:
             break;
     }
 }
Example #2
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="options"><see cref="anmar.SharpMimeTools.SharpDecodeOptions" /> 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, anmar.SharpMimeTools.MimeTopLevelMediaType types, SharpDecodeOptions options, System.String path, System.String preferredtextsubtype )
 {
     if ( path!=null && (options&SharpDecodeOptions.CreateFolder)!=SharpDecodeOptions.CreateFolder && !System.IO.Directory.Exists(path) ) {
     #if LOG
         if ( log.IsErrorEnabled )
             log.Error(System.String.Concat("Path [", path, "] does not exist and 'SharpDecodeOptions.CreateFolder' was not specified."));
     #endif
         path=null;
     }
     this.ParseMessage(message, types, options, preferredtextsubtype, path);
 }
Example #3
0
 private void ParseMessage( System.IO.Stream stream, anmar.SharpMimeTools.MimeTopLevelMediaType types, SharpDecodeOptions options, System.String preferredtextsubtype, System.String path )
 {
     this._attachments = new System.Collections.ArrayList();
     anmar.SharpMimeTools.SharpMimeMessage message = new anmar.SharpMimeTools.SharpMimeMessage(stream);
     this.ParseMessage(message, types, (options&SharpDecodeOptions.AllowHtml)==SharpDecodeOptions.AllowHtml, options, preferredtextsubtype, path);
     this._headers = message.Header;
     message.Close();
     message = null;
     // find and decode uuencoded content if configured to do so (and attachments a allowed)
     if ( (options&anmar.SharpMimeTools.SharpDecodeOptions.UuDecode)==anmar.SharpMimeTools.SharpDecodeOptions.UuDecode
            && (options&anmar.SharpMimeTools.SharpDecodeOptions.AllowAttachments)==anmar.SharpMimeTools.SharpDecodeOptions.AllowAttachments )
         this.UuDecode(path);
     // Date
     this._date = anmar.SharpMimeTools.SharpMimeTools.parseDate(this._headers.Date);
     if ( this._date.Equals(System.DateTime.MinValue) ) {
         System.String date = this._headers["Received"];
         if ( date==null )
             date = System.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 = System.String.Empty;
         this._date = anmar.SharpMimeTools.SharpMimeTools.parseDate(date);
     }
     // Subject
     this._subject = anmar.SharpMimeTools.SharpMimeTools.parserfc2047Header(this._headers.Subject);
     // To
     this._to = anmar.SharpMimeTools.SharpMimeAddressCollection.Parse(this._headers.To);
     // From
     anmar.SharpMimeTools.SharpMimeAddressCollection from = anmar.SharpMimeTools.SharpMimeAddressCollection.Parse(this._headers.From);
     foreach ( anmar.SharpMimeTools.SharpMimeAddress 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"];
     }
 }
Example #4
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="options"><see cref="anmar.SharpMimeTools.SharpDecodeOptions" /> 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>
 public SharpMessage( System.IO.Stream message, SharpDecodeOptions options, System.String path )
     : this(message, options, path, null)
 {
 }
Example #5
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="options"><see cref="anmar.SharpMimeTools.SharpDecodeOptions" /> 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, SharpDecodeOptions options, System.String path, System.String preferredtextsubtype )
 {
     anmar.SharpMimeTools.MimeTopLevelMediaType types = anmar.SharpMimeTools.MimeTopLevelMediaType.text|anmar.SharpMimeTools.MimeTopLevelMediaType.multipart|anmar.SharpMimeTools.MimeTopLevelMediaType.message;
     if ( (options&SharpDecodeOptions.AllowAttachments)==SharpDecodeOptions.AllowAttachments ) {
         types = types|anmar.SharpMimeTools.MimeTopLevelMediaType.application|anmar.SharpMimeTools.MimeTopLevelMediaType.audio|anmar.SharpMimeTools.MimeTopLevelMediaType.image|anmar.SharpMimeTools.MimeTopLevelMediaType.video;
     }
     if ( path!=null && (options&SharpDecodeOptions.CreateFolder)!=SharpDecodeOptions.CreateFolder && !System.IO.Directory.Exists(path) ) {
     #if LOG
         if ( log.IsErrorEnabled )
             log.Error(System.String.Concat("Path [", path, "] does not exist and 'SharpDecodeOptions.CreateFolder' was not specified."));
     #endif
         path=null;
     }
     this.ParseMessage(message, types, options, preferredtextsubtype, path);
 }
Example #6
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="options"><see cref="anmar.SharpMimeTools.SharpDecodeOptions" /> to determine how to do the decoding (must be combined as a bit map).</param>
 public SharpMessage( System.IO.Stream message, SharpDecodeOptions options )
     : this(message, options, null, null)
 {
 }
Example #7
0
 private void ParseMessage(SharpMimeMessage part, MimeTopLevelMediaType types, bool html, SharpDecodeOptions options, String preferredtextsubtype, 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"
             if (part.Header.TopLevelMediaType.Equals(MimeTopLevelMediaType.message))
             {
                 // Only message/rfc822 allowed, other subtypes ignored
                 if (part.Header.SubType == "rfc822")
                 {
                     // If NotRecursiveRfc822 option is set, handle part as an attachment
                     if ((options & SharpDecodeOptions.NotRecursiveRfc822) == SharpDecodeOptions.NotRecursiveRfc822)
                     {
                         goto case anmar.SharpMimeTools.MimeTopLevelMediaType.application;
                     }
                 }
                 else
                 {
                     break;
                 }
             }
             if (part.Header.SubType.Equals("alternative") && part.PartsCount > 0)
             {
                 SharpMimeMessage altenative = null;
                 // Get the first mime part of the alternatives that has a accepted Mime-Type
                 for (int i = part.PartsCount; i > 0; i--)
                 {
                     SharpMimeMessage 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 || (preferredtextsubtype != null && item.Header.SubType == preferredtextsubtype))
                         {
                             break;
                         }
                         // This one is preferred over the last part
                     }
                     else if (preferredtextsubtype != null && 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 & SharpDecodeOptions.NamedAnchors) == SharpDecodeOptions.NamedAnchors)
                     {
                         // There is a previous text body, so enclose it in <pre>
                         if (!HasHtmlBody && _body.Length > 0)
                         {
                             _body = String.Concat("<pre>", HttpUtility.HtmlEncode(_body), "</pre>");
                             HasHtmlBody = true;
                         }
                         // Add the anchor
                         _body = String.Concat(_body, "<a name=\"", SharpMimeTools.Rfc2392Url(MessageID), "_", SharpMimeTools.Rfc2392Url(part.Header.ContentID), "\"></a>");
                     }
                     ParseMessage(altenative, types, html, options, preferredtextsubtype, path);
                 }
                 // TODO: Take into account each subtype of "multipart" and "message"
             }
             else if (part.PartsCount > 0)
             {
                 foreach (SharpMimeMessage item in part)
                 {
                     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 = HasHtmlBody;
                 // HTML content not allowed
                 if (part.Header.SubType.Equals("html"))
                 {
                     if (!html)
                         break;
                     else
                         HasHtmlBody = true;
                 }
                 if (html && part.Header.Contains("Content-ID") && (options & SharpDecodeOptions.NamedAnchors) == SharpDecodeOptions.NamedAnchors)
                 {
                     HasHtmlBody = true;
                 }
                 if (HasHtmlBody && !body_was_html && !String.IsNullOrEmpty(_body))
                 {
                     _body = String.Concat("<pre>", HttpUtility.HtmlEncode(_body), "</pre>");
                 }
                 // If message body is html and this part has a Content-ID field
                 // add an anchor to mark this body part
                 if (HasHtmlBody && part.Header.Contains("Content-ID") && (options & SharpDecodeOptions.NamedAnchors) == SharpDecodeOptions.NamedAnchors)
                 {
                     _body = String.Concat(_body, "<a name=\"", SharpMimeTools.Rfc2392Url(MessageID), "_", SharpMimeTools.Rfc2392Url(part.Header.ContentID), "\"></a>");
                 }
                 if (HasHtmlBody && part.Header.SubType.Equals("plain"))
                 {
                     _body = String.Concat(_body, "<pre>", HttpUtility.HtmlEncode(part.BodyDecoded), "</pre>");
                 }
                 else
                     _body = String.Concat(_body, part.BodyDecoded);
             }
             else
             {
                 if ((types & MimeTopLevelMediaType.application) != MimeTopLevelMediaType.application)
                 {
                     return;
                 }
                 goto case anmar.SharpMimeTools.MimeTopLevelMediaType.application;
             }
             break;
         case MimeTopLevelMediaType.application:
         case MimeTopLevelMediaType.audio:
         case MimeTopLevelMediaType.image:
         case MimeTopLevelMediaType.video:
             // Attachments not allowed.
             if ((options & SharpDecodeOptions.AllowAttachments) != SharpDecodeOptions.AllowAttachments)
                 break;
             SharpAttachment attachment = null;
             // Save to a file
             if (path != null)
             {
                 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
             {
                 using (MemoryStream stream = new MemoryStream())
                 {
                     if (part.DumpBody(stream))
                     {
                         if (stream != null && stream.CanSeek)
                             stream.Seek(0, SeekOrigin.Begin);
                         attachment = new SharpAttachment(stream);
                         if (part.Name != null)
                             attachment.Name = part.Name;
                         else
                             attachment.Name = String.Concat("generated_", part.GetHashCode(), ".", part.Header.SubType);
                         attachment.Size = stream.Length;
                     }
                 }
             }
             if (attachment != null && part.Header.SubType == "ms-tnef" && (options & SharpDecodeOptions.DecodeTnef) == SharpDecodeOptions.DecodeTnef)
             {
                 // Try getting attachments form a tnef stream
                 Stream stream = attachment.Stream;
                 SharpTnefMessage tnef = new SharpTnefMessage(stream);
                 if (tnef.Parse(path))
                 {
                     if (tnef.Attachments != null)
                     {
                         Attachments.AddRange(tnef.Attachments);
                     }
                     attachment.Close();
                     // Delete the raw tnef file
                     if (attachment.SavedFile != null)
                     {
                         if (stream != null && stream.CanRead)
                         {
                             stream.Close();
                             stream = null;
                         }
                         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();
                         stream = null;
                     }
                 }
                 stream = null;
                 tnef = null;
             }
             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 = SharpMimeTools.parseDate(part.Header.ContentDispositionParameters["creation-date"]);
                 // Store attachment's LastWriteTime
                 if (part.Header.ContentDispositionParameters.ContainsKey("modification-date"))
                     attachment.LastWriteTime = SharpMimeTools.parseDate(part.Header.ContentDispositionParameters["modification-date"]);
                 if (part.Header.Contains("Content-ID"))
                     attachment.ContentID = part.Header.ContentID;
                 Attachments.Add(attachment);
             }
             break;
         default:
             break;
     }
 }
Example #8
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;
     }
 }
Example #9
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="options"><see cref="anmar.SharpMimeTools.SharpDecodeOptions" /> 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(Stream message, MimeTopLevelMediaType types, SharpDecodeOptions options, String path, String preferredtextsubtype)
 {
     if (path != null && (options & SharpDecodeOptions.CreateFolder) != SharpDecodeOptions.CreateFolder && !Directory.Exists(path))
     {
         path = null;
     }
     ParseMessage(message, types, options, preferredtextsubtype, path);
 }
Example #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="options"><see cref="anmar.SharpMimeTools.SharpDecodeOptions" /> 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(Stream message, SharpDecodeOptions options, String path, String preferredtextsubtype)
 {
     MimeTopLevelMediaType types;
     if ((options & SharpDecodeOptions.AllowAttachments) == SharpDecodeOptions.AllowAttachments)
         types = MimeTopLevelMediaType.text | MimeTopLevelMediaType.multipart | MimeTopLevelMediaType.message | MimeTopLevelMediaType.application | MimeTopLevelMediaType.audio | MimeTopLevelMediaType.image | MimeTopLevelMediaType.video;
     else
         types = MimeTopLevelMediaType.text | MimeTopLevelMediaType.multipart | MimeTopLevelMediaType.message;
     if (path != null && (options & SharpDecodeOptions.CreateFolder) != SharpDecodeOptions.CreateFolder && !Directory.Exists(path))
     {
         path = null;
     }
     ParseMessage(message, types, options, preferredtextsubtype, path);
 }