public static ParseMimeTypedAndEncodedContent ( string data ) : MimeTypedAndEncodedContent | ||
data | string | |
return | MimeTypedAndEncodedContent |
/*private static void ParseMessage(ref Message message) { ParseMessage(ref message, new X509Certificate2Collection()); }*/ private static Message ParseMessage(byte[] data) { string msg = System.Text.Encoding.ASCII.GetString(data); Message message = (Message)Parser.ParseMimeTypedAndEncodedContent(msg); foreach (string key in message.HeaderFields.AllKeys) { string name = key; string value = message.HeaderFields[key]; if (name.Equals("received")) message.Trace.Add(Parser.ParseTrace(key + ": " + value)); else if (name.Equals("to")) message.To = Parser.ParseAddresses(value); else if (name.Equals("cc")) message.Cc = Parser.ParseAddresses(value); else if (name.Equals("bcc")) message.Bcc = Parser.ParseAddresses(value); else if (name.Equals("reply-to")) message.ReplyTo = Parser.ParseAddress(value); else if (name.Equals("from")) message.From = Parser.ParseAddress(value); else if (name.Equals("sender")) message.Sender = Parser.ParseAddress(value); else if (name.Equals("content-type")) message.ContentType = Parser.GetContentType(key + ": " + value); else if (name.Equals("content-disposition")) message.ContentDisposition = Parser.GetContentDisposition(key + ": " + value); else if (name.Equals("domainkey-signature")) message.Signatures.DomainKeys = Signature.Parse(key + ": " + value, message); } return message; //if (message.ContentType.MimeType != null && message.ContentType.MimeType.IndexOf("multipart/signed") != -1) message.IsSigned = true; //if (message.ContentType.MimeType != null && message.ContentType.MimeType.IndexOf("multipart/encrypted") != -1) message.IsEncrypted = true; }
/*public static MimePart ParsePart(string data, X509Certificate2Collection certificates, SignedCms signature) { MimePart part = new MimePart(); if (signature.Certificates.Count != 0) part.IsSigned = true; part.Signature = signature; string header = data.Substring(0,Regex.Match(data, @"\r?\n\r?\n").Index); Match m = Regex.Match(header, @"(?<=((\r?\n)|\n)|\A)\S+:(.|(\r?\n[\t ]))+(?=((\r?\n)\S)|\Z)"); while (m.Success) { if (m.Value.ToLower().StartsWith("content-type:")) part.ContentType = Parser.GetContentType(m.Value); else if (m.Value.ToLower().StartsWith("content-disposition:")) part.ContentDisposition = Parser.GetContentDisposition(m.Value); part.HeaderFields.Add(FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':'))), m.Value.Substring(m.Value.IndexOf(':') + 1)); m = m.NextMatch(); } // S/MIME support // TODO : Differenciate by ASN.1 decoding if (part.ContentType.MimeType!=null && (part.ContentType.MimeType.IndexOf("application/x-pkcs7-mime") != -1 || part.ContentType.MimeType.IndexOf("application/pkcs7-mime") != -1)) { if (part.ContentType.Parameters["smime-type"]!=null && part.ContentType.Parameters["smime-type"].ToLower().IndexOf("signed-data") != -1) { part.IsSigned = true; string contentBase64 = data.Substring(Regex.Match(data, @"\r?\n\r?\n").Index); byte[] signedData = System.Convert.FromBase64String(contentBase64); SignedCms sig = new SignedCms(); sig.Decode(signedData); return ParsePart(System.Text.Encoding.ASCII.GetString(sig.ContentInfo.Content), certificates, sig); } else { part.IsEncrypted = true; string contentBase64 = data.Substring(Regex.Match(data, @"\r?\n\r?\n").Index); EnvelopedCms env = new EnvelopedCms(); env.Decode(System.Convert.FromBase64String(contentBase64)); env.Decrypt(certificates); byte[] decryptedContent = env.ContentInfo.Content; return ParsePart(System.Text.Encoding.ASCII.GetString(decryptedContent), certificates); } } if (part.Charset==null || part.Charset.Length < 1) part.Charset = "us-ascii"; byte[] binData = System.Text.Encoding.ASCII.GetBytes(data); try { part.TextContent = System.Text.Encoding.GetEncoding(part.Charset).GetString(binData); } catch { part.TextContent = data; } m = Regex.Match(part.TextContent,@"\r?\n\r?\n"); part.TextContent = part.TextContent.Substring(m.Index+m.Length); if (part.ContentType.MimeType != null && part.ContentType.MimeType.IndexOf("multipart/signed") != -1) part.IsSigned = true; if (part.ContentTransferEncoding==ContentTransferEncoding.QuotedPrintable) part.TextContent = Codec.FromQuotedPrintable(part.TextContent,part.Charset); if (part.ContentTransferEncoding == ContentTransferEncoding.Base64) { part.TextContent = part.TextContent.Trim('\r', '\n'); part.BinaryContent = System.Convert.FromBase64String(part.TextContent); } else part.BinaryContent = System.Text.Encoding.GetEncoding(part.Charset).GetBytes(part.TextContent); return part; }*/ private static MultipartContainer ParseMultipartContainer(MimePart part) { MultipartContainer container = new MultipartContainer(); string boundary = part.ContentType.Parameters["boundary"]; string[] arrpart = Regex.Split(part.TextContent, @"\r?\n?" + Regex.Escape("--" + boundary)); for (int i = 0; i < arrpart.Length; i++) { string strpart = arrpart[i]; if (!strpart.StartsWith("--")) { container.PartTree.Add(Parser.ParseMimeTypedAndEncodedContent(strpart)); } } return container; }