/// <summary> /// Parses a Message from a string formatted accordingly to the RFC822. /// </summary> /// <param name="data">The string containing the message data to be parsed.</param> /// <returns>The parsed message as a Message object.</returns> /// <example> /// <code> /// C# /// /// Message message = Parser.ParseMessageString(rfc822string); /// //Expose the subject /// string subject = message.Subject; /// /// VB.NET /// /// Dim message As Message = Parser.ParseMessageString(rfc822string) /// 'Expose the subject /// Dim subject As String = message.Subject /// /// JScript.NET /// /// var message:Message = Parser.ParseMessageString(rfc822string); /// //Expose the subject /// var subject:string = message.Subject; /// </code> /// </example> public static Message ParseMessage(string data) { #if !PocketPC return(ParseMessage(Encoding.GetEncoding("iso-8859-1").GetBytes(data))); #else return(Parser.ParseMessage(Pop3Client.PPCEncode.GetBytes(data))); #endif }
/// <summary> /// Parses a message from a file to a Message object. /// </summary> /// <param name="filePath">The path of the file to be parsed.</param> /// <returns>The parsed message as a Message object.</returns> /// <example> /// <code> /// C# /// /// Message message = Parser.ParseMessage("C:\\My messages\\message.txt"); /// //Expose the subject /// string subject = message.Subject; /// /// VB.NET /// /// Dim message As Message = Parser.ParseMessage("C:\My messages\message.txt") /// 'Expose the subject /// Dim subject As String = message.Subject /// /// JScript.NET /// /// var message:Message = Parser.ParseMessage("C:\\My messages\\message.txt"); /// //Expose the subject /// var subject:string = message.Subject; /// </code> /// </example> public static Message ParseMessageFromFile(string filePath) { System.IO.FileStream fs = System.IO.File.OpenRead(filePath); byte[] data = new byte[fs.Length]; fs.Read(data, 0, System.Convert.ToInt32(fs.Length)); fs.Close(); return(Parser.ParseMessage(data)); }
/*public static Message ParseMessageFromFile(string filePath, CertificateSelectionCallback certificateSelectionCallback) { Message msg = ParseMessageFromFile(filePath); if(msg.ContentType.Equals("application/pkcs7-mime") || msg.ContentType.Equals("application/x-pkcs7-mime") if(msg.SmimeType.Equals(SmimeType.EnvelopedData)) return Parser.ParseMessage(.certificateSelectionCallback.Invoke(msg.Recipients); } public delegate System.Security.Cryptography.X509Certificates.X509Certificate2 CertificateSelectionCallback(AddressCollection recipients); /*public static Message ParseMessageFromFile(string filePath, System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates) { }*/ /// <summary> /// Parses a MemoryStream's content to a Message object. /// </summary> /// <param name="inputStream">The MemoryStream containing the Header data to be parsed.</param> /// <returns>The parsed Header as a Message object.</returns> /// <example> /// <code> /// C# /// /// Message message = Parser.ParseMessage(someStream); /// //Expose the subject /// string subject = message.Subject; /// /// VB.NET /// /// Dim message As Message = Parser.ParseMessage(someStream) /// 'Expose the subject /// Dim subject As String = message.Subject /// /// JScript.NET /// /// var message:Message = Parser.ParseMessage(someStream); /// //Expose the subject /// var subject:string = message.Subject; /// </code> /// </example> public static Message ParseMessage(System.IO.MemoryStream inputStream) { byte[] buf = new byte[inputStream.Length]; inputStream.Read(buf,0,buf.Length); Message msg = new Message(); msg.OriginalData = buf; Parser.ParseMessage(buf);//ref msg); return msg; }
/// <summary> /// Parses a message from a file to a Message object. /// </summary> /// <param name="filePath">The path of the file to be parsed.</param> /// <returns>The parsed message as a Message object.</returns> /// <example> /// <code> /// C# /// /// Message message = Parser.ParseMessage("C:\\My messages\\message.txt"); /// //Expose the subject /// string subject = message.Subject; /// /// VB.NET /// /// Dim message As Message = Parser.ParseMessage("C:\My messages\message.txt") /// 'Expose the subject /// Dim subject As String = message.Subject /// /// JScript.NET /// /// var message:Message = Parser.ParseMessage("C:\\My messages\\message.txt"); /// //Expose the subject /// var subject:string = message.Subject; /// </code> /// </example> public static Message ParseMessageFromFile(string filePath, X509Certificate2Collection extraCertificates) { System.IO.FileStream fs = System.IO.File.OpenRead(filePath); byte[] data = new byte[fs.Length]; fs.Read(data, 0, System.Convert.ToInt32(fs.Length)); fs.Close(); Message msg = new Message(); msg.OriginalData = data; Parser.ParseMessage(ref msg, extraCertificates); return msg; }
private static void DispatchPart(MimePart part, ref Message message) { // This is a container part. if (part.SubParts.Count > 0) { DispatchParts(part, ref message); } // This is a leaf part. else { // If this part has to be displayed has an attachment, add it to the appropriate collection. if (part.ContentDisposition.Disposition.Equals("attachment")) { message.Attachments.Add(part); } // If this part has to be displayed at the same time as the main body, add it to the appropriate collection. else if (part.ContentDisposition.Disposition.Equals("inline")) { message.EmbeddedObjects.Add(part); } // Other parts are miscellaneous. How they are to be displayed is at the end-user's discretion. // Fix for avoid attach original mail message else if (!message.BodyText.ToMimePart().ContentTransferEncoding.Equals(part.ContentTransferEncoding)) { message.UnknownDispositionMimeParts.Add(part); } // We will consider the highest-level text parts that are not attachments to be the intended for display. // We know the highest-level parts will be set, because the parser first goes to the deepest level and returns top-level parts last. if (part.ContentType.Type.Equals("text") && !part.ContentDisposition.Disposition.Equals("attachment")) { if (part.ContentType.SubType.Equals("plain")) { message.BodyText.Charset = part.Charset; message.BodyText.Text = part.TextContent; } else if (part.ContentType.SubType.Equals("html")) { message.IsHtml = true; message.BodyHtml.Charset = part.Charset; message.BodyHtml.Text = part.TextContent; } } // Parse message/rfc822 parts as Message objects and place them in the appropriate collection. if (part.ContentType.MimeType.Equals("message/rfc822")) { message.SubMessages.Add(Parser.ParseMessage(part.BinaryContent)); } if (part.ContentType.MimeType.Equals("application/pkcs7-signature") || part.ContentType.MimeType.Equals("application/x-pkcs7-signature")) { string toDigest = part.Container.TextContent; toDigest = Regex.Split(toDigest, "\r\n--" + part.Container.ContentType.Parameters["boundary"])[1]; toDigest = toDigest.TrimStart('\r', '\n'); //Match endDelimiter = Regex.Match(toDigest, "(?<=[^\r\n]\r\n)\r\n", RegexOptions.RightToLeft); //int lastNonNewLine = Regex.Match(toDigest, "[^\r\n]", RegexOptions.RightToLeft).Index; //if (endDelimiter.Index != -1 && endDelimiter.Index > lastNonNewLine) toDigest = toDigest.Remove(endDelimiter.Index); //TODO: What should be done in PPC ? #if !PocketPC message.Signatures.Smime = new SignedCms(new ContentInfo(Encoding.ASCII.GetBytes(toDigest)), true); message.Signatures.Smime.Decode(part.BinaryContent); #endif } // Anyway, this is a leaf part of the message. message.LeafMimeParts.Add(part); } }
/*/// <summary> /// Parses a Message from a byte array. /// </summary> /// <param name="data">The byte array containing the message data to be parsed.</param> /// <returns>The parsed message as a Message object.</returns> /// <example> /// <code> /// C# /// /// Message message = Parser.ParseMessage(someBuffer); /// //Expose the subject /// string subject = message.Subject; /// /// VB.NET /// /// Dim message As Message = Parser.ParseMessage(someBuffer) /// 'Expose the subject /// Dim subject As String = message.Subject /// /// JScript.NET /// /// var message:Message = Parser.ParseMessage(someBuffer); /// //Expose the subject /// var subject:string = message.Subject; /// </code> /// </example> public static Message ParseMessage(byte[] data) { Message msg = new Message(); msg.OriginalData = data; Parser.ParseMessage(ref msg); return msg; }*/ /// <summary> /// Parses a Message from a string formatted accordingly to the RFC822. /// </summary> /// <param name="data">The string containing the message data to be parsed.</param> /// <returns>The parsed message as a Message object.</returns> /// <example> /// <code> /// C# /// /// Message message = Parser.ParseMessageString(rfc822string); /// //Expose the subject /// string subject = message.Subject; /// /// VB.NET /// /// Dim message As Message = Parser.ParseMessageString(rfc822string) /// 'Expose the subject /// Dim subject As String = message.Subject /// /// JScript.NET /// /// var message:Message = Parser.ParseMessageString(rfc822string); /// //Expose the subject /// var subject:string = message.Subject; /// </code> /// </example> public static Message ParseMessage(string data) { return Parser.ParseMessage(System.Text.Encoding.ASCII.GetBytes(data)); }
/// <summary> /// Retrieves the article at the position specified by the current article pointer. /// </summary> /// <returns>A Message object representing the article.</returns> /// <example> /// <code> /// C# /// /// NntpClient nntp = new NntpClient(); /// /// nntp.Connect("news.myhost.com"); /// /// NewsGroup group = nntp.SelectGroup("mygroup"); /// //Retrieve the first article in this group. /// Message article = group.RetrieveArticleObject(); /// /// nntp.Disconnect(); /// /// VB.NET /// /// Dim nntp As New NntpClient /// /// nntp.Connect("news.myhost.com") /// /// Dim group As NewsGroup = nntp.SelectGroup("mygroup") /// 'Retrieve the first article in this group. /// Dim article As Message = group.RetrieveArticleObject() /// /// nntp.Disconnect() /// /// JScript.NET /// /// var nntp:NntpClient = new NntpClient(); /// /// nntp.Connect("news.myhost.com"); /// /// var group:NewsGroup = nntp.SelectGroup("mygroup"); /// //Retrieve the first article in this group. /// var article:Message = group.RetrieveArticleObject(); /// nntp.Disconnect(); /// </code> /// </example> public Message RetrieveArticleObject() { return(Parser.ParseMessage(RetrieveArticle())); }
/// <summary> /// Retrieves the article at the specified ordinal position. /// </summary> /// <param name="index">The ordinal position of the article to be retrieved.</param> /// <returns>A Message object representing the article.</returns> /// <example> /// <code> /// C# /// /// NntpClient nntp = new NntpClient(); /// /// nntp.Connect("news.myhost.com"); /// /// NewsGroup group = nntp.SelectGroup("mygroup"); /// //Retrieve article at position 29 in this group. /// Message article29 = group.RetrieveArticleObject(29); /// //Retrieve last article in this group. /// Message article = group.RetrieveArticleObject(group.LastArticle); /// /// nntp.Disconnect(); /// /// VB.NET /// /// Dim nntp As New NntpClient /// /// nntp.Connect("news.myhost.com") /// /// Dim group As NewsGroup = nntp.SelectGroup("mygroup") /// 'Retrieve article at position 29 in this group. /// Dim article29 As Message = group.RetrieveArticleObject(29) /// 'Retrieve last article in this group. /// Dim article As Message = group.RetrieveArticleObject(group.LastArticle) /// /// nntp.Disconnect() /// /// JScript.NET /// /// var nntp:NntpClient = new NntpClient(); /// /// nntp.Connect("news.myhost.com"); /// /// var group:NewsGroup = nntp.SelectGroup("mygroup"); /// //Retrieve article at position 29 in this group. /// var article29:Message = group.RetrieveArticleObject(29); /// //Retrieve last article in this group. /// var article:Message = group.RetrieveArticleObject(group.LastArticle); /// /// nntp.Disconnect(); /// </code> /// </example> public Message RetrieveArticleObject(int index) { return(Parser.ParseMessage(RetrieveArticle(index))); }