/// <summary> /// Parses the message. /// </summary> /// <param name="data">The data.</param> /// <param name="compact">removes the original content string from mime parts. To get the string later, use <see cref="MimePart.DecodeOriginalContent()"/></param> /// <returns></returns> public static Message ParseMessage(byte[] data, bool compact = false) { Message message = new Message(); try { // Build a part tree and get all headers. MimePart part = ParseMimePart(data, message, compact); // Fill a new message object with the new information. message.OriginalData = data; message.HeaderFields = part.HeaderFields; message.HeaderFieldNames = part.HeaderFieldNames; // Dispatch header fields to corresponding object. foreach (string key in message.HeaderFields.AllKeys) { string value = message.HeaderFields[key]; if (key.Equals("received")) { Match m = Regex.Match(value, @"from.+?(?=(from|$))"); while (m.Success) { message.Trace.Add(ParseTrace(key + ": " + m.Value)); m = m.NextMatch(); } } else if (key.Equals("to")) { message.To = ParseAddresses(value); } else if (key.Equals("cc")) { message.Cc = ParseAddresses(value); } else if (key.Equals("bcc")) { message.Bcc = ParseAddresses(value); } else if (key.Equals("reply-to")) { message.ReplyTo = ParseAddress(value); } else if (key.Equals("from")) { message.From = ParseAddress(value); } else if (key.Equals("sender")) { message.Sender = ParseAddress(value); } else if (key.Equals("content-type")) { message.ContentType = GetContentType(key + ": " + value); } else if (key.Equals("content-disposition")) { message.ContentDisposition = GetContentDisposition(key + ": " + value); } else if (key.Equals("domainkey-signature")) { message.Signatures.DomainKeys = Signature.Parse(key + ": " + value, message); } } if (message.ContentType.MimeType.Equals("application/pkcs7-mime") || message.ContentType.MimeType.Equals("application/x-pkcs7-mime")) { if (message.ContentType.Parameters["smime-type"] != null && message.ContentType.Parameters["smime-type"].Equals("enveloped-data")) { message.IsSmimeEncrypted = true; } if (message.ContentType.Parameters["smime-type"] != null && message.ContentType.Parameters["smime-type"].Equals("signed-data")) { message.HasSmimeSignature = true; } } if (message.ContentType.MimeType.Equals("multipart/signed")) { message.HasSmimeDetachedSignature = true; } // Keep a reference to the part tree within the new Message object. message.PartTreeRoot = part; DispatchParts(ref message); // Dispatch the part tree content to the appropriate collections and properties. // TODO } catch (Exception ex) { ErrorParsing?.Invoke(null, ex); } return(message); }
protected void OnErrorParsing(EnumErrors error) { ErrorParsing?.Invoke(this, error); Stop(); }