public static ParseAddress ( string input ) : ActiveUp.Net.Mail.Address | ||
input | string | |
Résultat | ActiveUp.Net.Mail.Address |
public static void ParseHeader(ref Header header) { string hdr = System.Text.Encoding.ASCII.GetString(header.OriginalData); hdr = System.Text.RegularExpressions.Regex.Match(hdr, @"[\s\S]+?((?=\r?\n\r?\n)|\Z)").Value; hdr = Parser.Unfold(hdr); hdr = Codec.RFC2047Decode(hdr); System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(hdr, @"(?<=((\r?\n)|\n)|\A)\S+:(.|(\r?\n[\t ]))+(?=((\r?\n)\S)|\Z)"); while(m.Success) { string name = FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':'))); string value = m.Value.Substring(m.Value.IndexOf(":") + 1); if (name.Equals("received")) header.Trace.Add(Parser.ParseTrace(m.Value.Trim(' '))); else if (name.Equals("to")) header.To = Parser.ParseAddresses(value); else if (name.Equals("cc")) header.Cc = Parser.ParseAddresses(value); else if (name.Equals("bcc")) header.Bcc = Parser.ParseAddresses(value); else if (name.Equals("reply-to")) header.ReplyTo = Parser.ParseAddress(value); else if (name.Equals("from")) header.From = Parser.ParseAddress(value); else if (name.Equals("sender")) header.Sender = Parser.ParseAddress(value); else if (name.Equals("content-type")) header.ContentType = Parser.GetContentType(m.Value); else if (name.Equals("content-disposition")) header.ContentDisposition = Parser.GetContentDisposition(m.Value); header.HeaderFields.Add(name, value); header.HeaderFieldNames.Add(name, m.Value.Substring(0, m.Value.IndexOf(':'))); m = m.NextMatch(); } }
/// <summary> /// Parses a string containing addresses in the following formats : /// <list type="circle"> /// <item>"John Doe" <[email protected]>,"Mike Johns" <[email protected]></item> /// <item>"John Doe" <[email protected]>;"Mike Johns" <[email protected]></item> /// <item><[email protected]></item> /// <item>[email protected]</item> /// </list> /// </summary> /// <param name="input">A string containing addresses in the formats desribed above.</param> /// <returns>An AddressCollection object containing the parsed addresses.</returns> public static AddressCollection ParseAddresses(string input) { //TODO: enforce parser to use regex AddressCollection addresses = new AddressCollection(); string[] comma_separated = input.Split(','); for (int i = 0; i < comma_separated.Length; i++) { if (comma_separated[i].IndexOf("@") == -1 && comma_separated.Length > (i + 1)) { comma_separated[i + 1] = comma_separated[i] + comma_separated[i + 1]; } } for (int i = 0; i < comma_separated.Length; i++) { if (comma_separated[i].IndexOf("@") != -1) { addresses.Add(Parser.ParseAddress((comma_separated[i].IndexOf("<") != -1 && comma_separated[i].IndexOf(":") != -1 && comma_separated[i].IndexOf(":") < comma_separated[i].IndexOf("<")) ? ((comma_separated[i].Split(':')[0].IndexOf("\"") == -1) ? comma_separated[i].Split(':')[1] : comma_separated[i]) : comma_separated[i])); } } //MatchCollection matches = Regex.Matches(input, "(\"(?<name>.+?)\")*\\s*<?(?<email>[^<>,\"\\s]+)>?"); //foreach (Match m in matches) // addresses.Add(m.Groups["email"].Value, m.Groups["name"].Value); return(addresses); }
/*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; }
/// <summary> /// Creates the Address using the specified Internet email (RFC 2822 addr-spec). /// </summary> /// <param name="email">The email address to use.</param> public Address(string email) { Address addr = Parser.ParseAddress(email); Email = addr.Email; Name = addr.Name; }
/// <summary> /// Creates the Address using the specified Internet email (RFC 2822 addr-spec). /// </summary> /// <param name="email">The email address to use.</param> public Address(string email) { /*this.Email = * this.Name = string.Empty;*/ Address addr = Parser.ParseAddress(email); this.Email = addr.Email; this.Name = addr.Name; }
//Address parsing conformant to RFC2822's addr-spec. /// <summary> /// Parses a string containing addresses in the following formats : /// <list type="circle"> /// <item>"John Doe" <[email protected]>,"Mike Johns" <[email protected]></item> /// <item>"John Doe" <[email protected]>;"Mike Johns" <[email protected]></item> /// <item><[email protected]></item> /// <item>[email protected]</item> /// </list> /// </summary> /// <param name="input">A string containing addresses in the formats desribed above.</param> /// <returns>An AddressCollection object containing the parsed addresses.</returns> public static AddressCollection ParseAddresses(string input) { AddressCollection addresses = new AddressCollection(); string[] comma_separated = input.Split(','); for(int i=0;i<comma_separated.Length;i++) if(comma_separated[i].IndexOf("@")==-1 && comma_separated.Length>(i+1)) comma_separated[i+1] = comma_separated[i]+comma_separated[i+1]; for(int i=0;i<comma_separated.Length;i++) /*if(comma_separated[i].IndexOf("@")!=-1)*/ addresses.Add(Parser.ParseAddress((comma_separated[i].IndexOf("<")!=-1 && comma_separated[i].IndexOf(":")!=-1 && comma_separated[i].IndexOf(":")<comma_separated[i].IndexOf("<")) ? ((comma_separated[i].Split(':')[0].IndexOf("\"")==-1) ? comma_separated[i].Split(':')[1] : comma_separated[i]) : comma_separated[i])); return addresses; }
/// <summary> /// Parses the message. /// </summary> /// <param name="data">The data.</param> /// <returns></returns> public static Message ParseMessage(byte[] data) { //string msg = System.Text.Encoding.ASCII.GetString(data); #if !PocketPC string msg = System.Text.Encoding.UTF8.GetString(data, 0, data.Length); #else string msg = Pop3Client.PPCEncode.GetString(data, 0, data.Length); #endif Message message = new Message(); try { // Build a part tree and get all headers. MimePart part = ParseMimePart(msg, message); // 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 name = key; string value = message.HeaderFields[key]; // TODO : Fix trace 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); } } 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) { if (ErrorParsing != null) { ErrorParsing(null, ex); } } return(message); }
/// <summary> /// Parses the header. /// </summary> /// <param name="header">The header.</param> public static void ParseHeader(ref Header header) { #if !PocketPC string hdr = System.Text.Encoding.GetEncoding("iso-8859-1").GetString(header.OriginalData, 0, header.OriginalData.Length); #else string hdr = Pop3Client.PPCEncode.GetString(header.OriginalData, 0, header.OriginalData.Length); #endif hdr = System.Text.RegularExpressions.Regex.Match(hdr, @"[\s\S]+?((?=\r?\n\r?\n)|\Z)").Value; hdr = Parser.Unfold(hdr); //hdr = hdr); System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(hdr, @"(?<=((\r?\n)|\n)|\A)\S+:(.|(\r?\n[\t ]))+(?=((\r?\n)\S)|\Z)"); while (m.Success) { string name = FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':'))); string value = Codec.RFC2047Decode(m.Value.Substring(m.Value.IndexOf(":") + 1)).Trim('\r', '\n').TrimStart(' '); if (name.Equals("received")) { header.Trace.Add(Parser.ParseTrace(m.Value.Trim(' '))); } else if (name.Equals("to")) { header.To = Parser.ParseAddresses(value); } else if (name.Equals("cc")) { header.Cc = Parser.ParseAddresses(value); } else if (name.Equals("bcc")) { header.Bcc = Parser.ParseAddresses(value); } else if (name.Equals("reply-to")) { header.ReplyTo = Parser.ParseAddress(value); } else if (name.Equals("from")) { header.From = Parser.ParseAddress(value); } else if (name.Equals("sender")) { header.Sender = Parser.ParseAddress(value); } else if (name.Equals("content-type")) { header.ContentType = Parser.GetContentType(m.Value); } else if (name.Equals("content-disposition")) { header.ContentDisposition = Parser.GetContentDisposition(m.Value); } //else //{ header.HeaderFields.Add(name, value); header.HeaderFieldNames.Add(name, m.Value.Substring(0, m.Value.IndexOf(':'))); //} m = m.NextMatch(); if (HeaderFieldParsing != null) { HeaderFieldParsing(null, header); } } }
private static void ParseMessage(ref Message message, X509Certificate2Collection certificates) { // Header parsing work string msg = System.Text.Encoding.ASCII.GetString(message.OriginalData); string header = msg.Substring(0,Regex.Match(msg, @"\r?\n\r?\n").Index); header = Parser.Unfold(header); header = Codec.RFC2047Decode(header); Match m = Regex.Match(header, @"(?<=((\r?\n)|\n)|\A)\S+:(.|(\r?\n[\t ]))+(?=((\r?\n)\S)|\Z)"); while (m.Success) { string name = FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':'))); string value = m.Value.Substring(m.Value.IndexOf(":") + 1); if (name.Equals("received")) message.Trace.Add(Parser.ParseTrace(m.Value.Trim(' '))); 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(m.Value); else if (name.Equals("content-disposition")) message.ContentDisposition = Parser.GetContentDisposition(m.Value); else if (name.Equals("domainkey-signature")) message.Signatures.DomainKeys = Signature.Parse(m.Value,message); message.HeaderFields.Add(name,value); message.HeaderFieldNames.Add(name, m.Value.Substring(0, m.Value.IndexOf(':'))); m = m.NextMatch(); } // Body parsing work 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; //MimeTypedAndEncodedContent part = Parser.ParseMimeTypedAndEncodedContent(msg);//, certificates); /*if (part.ContentType.MimeType!=null && part.ContentType.MimeType.IndexOf("multipart/") != -1) { Parser.ParseMultipart(ref message, part, certificates); Parser.DistributeParts(ref message); Parser.SetBodies(ref message); } else if (part.ContentDisposition.Disposition.Equals("attachment")) { message.AllMimeParts.Add(part); message.Attachments.Add(part); } else { message.AllMimeParts.Add(part); if (part.ContentType.MimeType!=null && part.ContentType.MimeType.ToLower().IndexOf("text/html") != -1) { if (part.ContentTransferEncoding == ContentTransferEncoding.Base64) message.BodyHtml.Text = System.Text.Encoding.GetEncoding(part.Charset).GetString(System.Convert.FromBase64String(part.TextContent.Substring(0, part.TextContent.IndexOf("\r\n\r\n")))); else message.BodyHtml.Text = part.TextContent; } else if (part.ContentType.MimeType != null && part.ContentType.MimeType.ToLower().IndexOf("text/plain") != -1) { if (part.ContentTransferEncoding == ContentTransferEncoding.Base64) message.BodyText.Text = System.Text.Encoding.GetEncoding(part.Charset).GetString(System.Convert.FromBase64String(part.TextContent.Substring(0, part.TextContent.IndexOf("\r\n\r\n")))); else message.BodyText.Text = part.TextContent; } else { message.BodyText.Text = System.Text.Encoding.ASCII.GetString(message.BodyBinary); } } if (!ActiveUp.Base.InternalLicense.Status.IsRegistered || ActiveUp.Base.InternalLicense.IsLite()) { if (message.BodyHtml.Text.Length > 0 && message.BodyHtml.Text.IndexOf(ActiveUp.Base.InternalLicense.UnRegisteredHtmlSent) == -1 && message.BodyHtml.Text.IndexOf(ActiveUp.Base.InternalLicense.SponsorHtmlSent) == -1 && message.BodyHtml.Text.IndexOf(ActiveUp.Base.InternalLicense.UnRegisteredHtmlReceived) == -1) message.BodyHtml.Text = ActiveUp.Base.InternalLicense.UnRegisteredHtmlReceived + "<br><br>" + message.BodyHtml.Text; if ((message.BodyText.Text.Length > 0 || message.BodyHtml.Text.Length == 0) && message.BodyText.Text.IndexOf(ActiveUp.Base.InternalLicense.UnRegisteredTextSent) == -1 && message.BodyText.Text.IndexOf(ActiveUp.Base.InternalLicense.SponsorTextSent) == -1 && message.BodyText.Text.IndexOf(ActiveUp.Base.InternalLicense.UnRegisteredTextReceived) == -1) message.BodyText.Text = ActiveUp.Base.InternalLicense.UnRegisteredTextReceived + "\n\n" + message.BodyText.Text; } else if (ActiveUp.Base.InternalLicense.IsSponsored()) { if (message.BodyHtml.Text.Length > 0 && message.BodyHtml.Text.IndexOf(ActiveUp.Base.InternalLicense.SponsorHtmlSent) == -1 && message.BodyHtml.Text.IndexOf(ActiveUp.Base.InternalLicense.SponsorHtmlReceived) == -1 && message.BodyHtml.Text.IndexOf(ActiveUp.Base.InternalLicense.UnRegisteredHtmlSent) == -1) message.BodyHtml.Text += "<br><br>" + ActiveUp.Base.InternalLicense.SponsorHtmlReceived; if ((message.BodyText.Text.Length > 0 || message.BodyHtml.Text.Length == 0) && message.BodyText.Text.IndexOf(ActiveUp.Base.InternalLicense.SponsorTextSent) == -1 && message.BodyText.Text.IndexOf(ActiveUp.Base.InternalLicense.SponsorTextReceived) == -1 && message.BodyHtml.Text.IndexOf(ActiveUp.Base.InternalLicense.UnRegisteredTextSent) == -1) message.BodyText.Text += "\n\n" + ActiveUp.Base.InternalLicense.SponsorTextReceived; }*/ }