/// <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]; } } foreach (string t in comma_separated) { if (t.IndexOf("@") != -1) { addresses.Add(ParseAddress((t.IndexOf("<") != -1 && t.IndexOf(":") != -1 && t.IndexOf(":") < t.IndexOf("<")) ? ((t.Split(':')[0].IndexOf("\"") == -1) ? t.Split(':')[1] : t) : t)); } } //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); }
/// <summary> /// Allows the developer to add a collection of Address objects in another one. /// </summary> /// <param name="first">The first collection.</param> /// <param name="second">The second collection.</param> /// <returns>The concatened collection.</returns> public static AddressCollection operator +(AddressCollection first, AddressCollection second) { AddressCollection newAddresses = first; foreach (Address address in second) { newAddresses.Add(address); } return(newAddresses); }
/// <summary> /// Validates the addresses' syntax. /// </summary> /// <param name="address">The addresses to be validated.</param> /// <returns>True if syntax is valid, otherwise false.</returns> public static ActiveUp.Net.Mail.AddressCollection ValidateSyntax(ActiveUp.Net.Mail.AddressCollection addresses) { ActiveUp.Net.Mail.AddressCollection invalids = new ActiveUp.Net.Mail.AddressCollection(); foreach (ActiveUp.Net.Mail.Address address in addresses) { if (!ActiveUp.Net.Mail.Validator.ValidateSyntax(address.Email)) { invalids.Add(address); } } return(invalids); }
//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> /// Validates the addresses' syntax. /// </summary> /// <param name="address">The addresses to be validated.</param> /// <returns>True if syntax is valid, otherwise false.</returns> public static AddressCollection ValidateSyntax(AddressCollection addresses) { AddressCollection invalids = new AddressCollection(); foreach (Address address in addresses) { if (!ValidateSyntax(address.Email)) { invalids.Add(address); } } return(invalids); }
/// <summary> /// Validates the addresses' syntax. /// </summary> /// <param name="address">The addresses to be validated.</param> /// <returns>True if syntax is valid, otherwise false.</returns> public static ActiveUp.Net.Mail.AddressCollection ValidateSyntax(ActiveUp.Net.Mail.AddressCollection addresses) { ActiveUp.Net.Mail.AddressCollection invalids = new ActiveUp.Net.Mail.AddressCollection(); foreach(ActiveUp.Net.Mail.Address address in addresses) if(!ActiveUp.Net.Mail.Validator.ValidateSyntax(address.Email)) invalids.Add(address); return invalids; }
/// <summary> /// Performs a VRFY command on the server using the specified addresses (checks if the addresses refer to mailboxes on the server). /// </summary> /// <param name="address">The addresses to be verified.</param> /// <returns>A collection containing the invalid addresses.</returns> /// <example> /// <code> /// C# /// /// SmtpClient smtp = new SmtpClient(); /// smtp.Connect("mail.myhost.com",8504); /// try /// { /// smtp.Ehlo(); /// } /// catch /// { /// smtp.Helo(); /// } /// //Create a collection to test. /// AddressCollection myaddresses = new AddressCollection(); /// myaddresses.Add("*****@*****.**","John Doe"); /// myaddresses.Add("*****@*****.**","Mike Johns"); /// //Verifies all addresses. /// AddressCollection invalidAddresses = smtp.Verify(myaddresses); /// smtp.Disconnect(); /// /// VB.NET /// /// Dim smtp As New SmtpClient /// smtp.Connect("mail.myhost.com",8504) /// Try /// smtp.Ehlo() /// Catch /// smtp.Helo() /// End Try /// 'Create a collection to test. /// Dim myaddresses As New AddressCollection /// myaddresses.Add("*****@*****.**","John Doe") /// myaddresses.Add("*****@*****.**","Mike Johns") /// 'Verifies all addresses. /// Dim invalidAddresses As AddressCollection = smtp.Verify(myaddresses) /// smtp.Disconnect() /// /// JScript.NET /// /// var smtp:SmtpClient = new SmtpClient(); /// smtp.Connect("mail.myhost.com",8504); /// try /// { /// smtp.Ehlo(); /// } /// catch /// { /// smtp.Helo(); /// } /// //Create a collection to test. /// var myaddresses:AddressCollection = new AddressCollection(); /// myaddresses.Add("*****@*****.**","John Doe"); /// myaddresses.Add("*****@*****.**","Mike Johns"); /// //Verifies all addresses. /// var invalidAddresses:AddressCollection = smtp.Verify(myaddresses); /// smtp.Disconnect(); /// </code> /// </example> public ActiveUp.Net.Mail.AddressCollection Verify(ActiveUp.Net.Mail.AddressCollection addresses) { ActiveUp.Net.Mail.AddressCollection incorrects = new ActiveUp.Net.Mail.AddressCollection(); foreach(ActiveUp.Net.Mail.Address address in addresses) { try { this.Verify(address.Email); } catch { incorrects.Add(address); } } return incorrects; }
/// <summary> /// Validates syntax and existence of the given address and returns valid addresses. /// </summary> /// <param name="addresses">The collection to be filtered.</param> /// <param name="dnsServers">Name Servers to be used for MX records search.</param> /// <returns>A collection containing the valid addresses.</returns> public static AddressCollection Filter(AddressCollection addresses, ServerCollection dnsServers) { AddressCollection valids = new AddressCollection(); AddressCollection valids1 = new AddressCollection(); System.Collections.Specialized.HybridDictionary ads = new System.Collections.Specialized.HybridDictionary(); for (int i = 0; i < addresses.Count; i++) { if (ValidateSyntax(addresses[i].Email)) { valids.Add(addresses[i]); } } #if !PocketPC Array domains = Array.CreateInstance(typeof(string), new int[] { valids.Count }, new int[] { 0 }); Array adds = Array.CreateInstance(typeof(Address), new int[] { valids.Count }, new int[] { 0 }); #else System.Array domains = System.Array.CreateInstance(typeof(string), new int[] { valids.Count }); System.Array adds = System.Array.CreateInstance(typeof(ActiveUp.Net.Mail.Address), new int[] { valids.Count }); #endif for (int i = 0; i < valids.Count; i++) { domains.SetValue(valids[i].Email.Split('@')[1], i); adds.SetValue(valids[i], i); } Array.Sort(domains, adds, null); string currentDomain = ""; string address = ""; SmtpClient smtp = new SmtpClient(); bool isConnected = false; for (int i = 0; i < adds.Length; i++) { address = ((Address)adds.GetValue(i)).Email; if (((string)domains.GetValue(i)) == currentDomain) { if (!smtp.Verify(address)) { try { //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName()); //smtp.MailFrom("postmaster@"+currentDomain); smtp.RcptTo(address); valids1.Add((Address)adds.GetValue(i)); } catch { } } else { valids1.Add((Address)adds.GetValue(i)); } } else { currentDomain = (string)domains.GetValue(i); try { if (isConnected == true) { isConnected = false; smtp.Disconnect(); smtp = new SmtpClient(); } smtp.Connect(GetMxRecords(currentDomain, dnsServers).GetPrefered().Exchange); isConnected = true; try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } if (!smtp.Verify(address)) { try { //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName()); //smtp.MailFrom("*****@*****.**"); smtp.MailFrom("postmaster@" + currentDomain); smtp.RcptTo(address); valids1.Add((Address)adds.GetValue(i)); } catch { } } else { valids1.Add((Address)adds.GetValue(i)); } } catch { } } } if (isConnected == true) { smtp.Disconnect(); } return(valids1); }
/// <summary> /// Validates syntax and existence of the given address and returns valid addresses. /// </summary> /// <param name="addresses">The collection to be filtered.</param> /// <param name="dnsServers">Name Servers to be used for MX records search.</param> /// <returns>A collection containing the valid addresses.</returns> public static AddressCollection Filter(AddressCollection addresses, ServerCollection dnsServers) { ActiveUp.Net.Mail.AddressCollection valids = new ActiveUp.Net.Mail.AddressCollection(); ActiveUp.Net.Mail.AddressCollection valids1 = new ActiveUp.Net.Mail.AddressCollection(); System.Collections.Specialized.HybridDictionary ads = new System.Collections.Specialized.HybridDictionary(); for (int i = 0; i < addresses.Count; i++) if (ActiveUp.Net.Mail.Validator.ValidateSyntax(addresses[i].Email)) valids.Add(addresses[i]); #if !PocketPC System.Array domains = System.Array.CreateInstance(typeof(string), new int[] { valids.Count }, new int[] { 0 }); System.Array adds = System.Array.CreateInstance(typeof(ActiveUp.Net.Mail.Address), new int[] { valids.Count }, new int[] { 0 }); #else System.Array domains = System.Array.CreateInstance(typeof(string), new int[] { valids.Count }); System.Array adds = System.Array.CreateInstance(typeof(ActiveUp.Net.Mail.Address), new int[] { valids.Count }); #endif for (int i = 0; i < valids.Count; i++) { domains.SetValue(valids[i].Email.Split('@')[1], i); adds.SetValue(valids[i], i); } System.Array.Sort(domains, adds, null); string currentDomain = ""; string address = ""; ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); bool isConnected = false; for (int i = 0; i < adds.Length; i++) { address = ((ActiveUp.Net.Mail.Address)adds.GetValue(i)).Email; if (((string)domains.GetValue(i)) == currentDomain) { if (!smtp.Verify(address)) { try { //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName()); //smtp.MailFrom("postmaster@"+currentDomain); smtp.RcptTo(address); valids1.Add((ActiveUp.Net.Mail.Address)adds.GetValue(i)); } catch { } } else valids1.Add((ActiveUp.Net.Mail.Address)adds.GetValue(i)); } else { currentDomain = (string)domains.GetValue(i); try { if (isConnected == true) { isConnected = false; smtp.Disconnect(); smtp = new ActiveUp.Net.Mail.SmtpClient(); } smtp.Connect(ActiveUp.Net.Mail.Validator.GetMxRecords(currentDomain, dnsServers).GetPrefered().Exchange); isConnected = true; try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } if (!smtp.Verify(address)) { try { //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName()); //smtp.MailFrom("*****@*****.**"); smtp.MailFrom("postmaster@" + currentDomain); smtp.RcptTo(address); valids1.Add((ActiveUp.Net.Mail.Address)adds.GetValue(i)); } catch { } } else valids1.Add((ActiveUp.Net.Mail.Address)adds.GetValue(i)); } catch { } } } if (isConnected == true) smtp.Disconnect(); return valids1; }