/// <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); }
/// <summary> /// Replace in an AddressCollection. /// </summary> /// <param name="addresses">The AddressCollection.</param> /// <param name="field">The field.</param> /// <param name="replacement">The replacement string.</param> /// <returns>The AddressCollection with replaced content.</returns> private ActiveUp.Net.Mail.AddressCollection ReplaceInAddresses(ActiveUp.Net.Mail.AddressCollection addresses, string field, string replacement) { for (int index = 0; index < addresses.Count; index++) { addresses[index].Email = ReplaceField(addresses[index].Email, field, replacement); addresses[index].Name = ReplaceField(addresses[index].Name, field, replacement); } return(addresses); }
/// <summary> /// Replace in an AddressCollection. /// </summary> /// <param name="addresses">The AddressCollection.</param> /// <param name="field">The field.</param> /// <param name="replacement">The replacement string.</param> /// <returns>The AddressCollection with replaced content.</returns> private ActiveUp.Net.Mail.AddressCollection ReplaceInAddresses(ActiveUp.Net.Mail.AddressCollection addresses, string field, string replacement) { foreach (Address t in addresses) { t.Email = ReplaceField(t.Email, field, replacement); t.Name = ReplaceField(t.Name, field, replacement); } 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> /// Merge the address collection with the specified item. /// </summary> /// <param name="addresses">The address to merge.</param> /// <param name="item">The item to use for merging.</param> /// <returns>The merged Address colection.</returns> public AddressCollection MergeAddresses(AddressCollection addresses, object item) { int index; for (index = 0; index < addresses.Count; index++) { addresses[index].Email = MergeText(addresses[index].Email, item); addresses[index].Name = MergeText(addresses[index].Name, item); } 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 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> /// Merge the Address collection with the specified datasource. /// </summary> /// <param name="addresses">The addresses to merge.</param> /// <param name="dataSource">The datasource to use for merging.</param> /// <param name="repeat">Specify if the texts will be repeated or not.</param> /// <returns>The merged Address collection</returns> public AddressCollection MergeAddresses(AddressCollection addresses, object dataSource, bool repeat) { int index; for (index = 0; index < addresses.Count; index++) { addresses[index].Email = MergeText(addresses[index].Email, dataSource, repeat); addresses[index].Name = MergeText(addresses[index].Name, dataSource, repeat); } 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); }
//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; }
public static IAsyncResult BeginFilter(AddressCollection addresses, ServerCollection dnsServers, AsyncCallback callback) { _delegateFilterServers = Filter; return(_delegateFilterServers.BeginInvoke(addresses, dnsServers, callback, _delegateFilterServers)); }
public static IAsyncResult BeginGetInvalidAddresses(AddressCollection addresses, AsyncCallback callback) { SmtpValidator._delegateGetInvalidAddresses = SmtpValidator.GetInvalidAddresses; return(SmtpValidator._delegateGetInvalidAddresses.BeginInvoke(addresses, callback, SmtpValidator._delegateGetInvalidAddresses)); }
/// <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> /// Sends the message using the specified DNS servers to get mail exchange servers addresses. /// </summary> /// <param name="message">The message to be sent.</param> /// <param name="dnsServers">Servers to be used (in preference order).</param> /// <example> /// <code> /// C# /// /// Message message = new Message(); /// message.Subject = "Test"; /// message.From = new Address("*****@*****.**","John Doe"); /// message.To.Add("*****@*****.**","Mike Johns"); /// message.BodyText.Text = "Hello this is a test!"; /// /// ServerCollection servers = new ServerCollection(); /// servers.Add("ns1.dnsserver.com",53); /// servers.Add("ns2.dnsserver.com",53); /// /// SmtpClient.DirectSend(message,servers); /// /// VB.NET /// /// Dim message As New Message /// message.Subject = "Test" /// message.From = New Address("*****@*****.**","John Doe") /// message.To.Add("*****@*****.**","Mike Johns") /// message.BodyText.Text = "Hello this is a test!" /// /// Dim servers As New ServerCollection /// servers.Add("ns1.dnsserver.com",53) /// servers.Add("ns2.dnsserver.com",53) /// /// SmtpClient.DirectSend(message,servers) /// /// JScript.NET /// /// var message:Message = new Message(); /// message.Subject = "Test"; /// message.From = new Address("*****@*****.**","John Doe"); /// message.To.Add("*****@*****.**","Mike Johns"); /// message.BodyText.Text = "Hello this is a test!"; /// /// var servers:ServerCollection = new ServerCollection(); /// servers.Add("ns1.dnsserver.com",53); /// servers.Add("ns2.dnsserver.com",53); /// /// SmtpClient.DirectSend(message,servers); /// </code> /// </example> public static string DirectSend(Message message, ServerCollection dnsServers) { // Ensure that the mime part tree is built message.CheckBuiltMimePartTree(); string email = (message.From.Name!="(unknown)") ? message.From.Email : message.Sender.Email; int recipientCount = message.To.Count+message.Cc.Count+message.Bcc.Count; #if !PocketPC System.Array domains = System.Array.CreateInstance(typeof(string),new int[] {recipientCount},new int[] {0}); System.Array adds = System.Array.CreateInstance(typeof(ActiveUp.Net.Mail.Address),new int[] {recipientCount},new int[] {0}); #else System.Array domains = System.Array.CreateInstance(typeof(string), new int[] { recipientCount }); System.Array adds = System.Array.CreateInstance(typeof(ActiveUp.Net.Mail.Address), new int[] { recipientCount }); #endif ActiveUp.Net.Mail.AddressCollection recipients = new ActiveUp.Net.Mail.AddressCollection(); recipients += message.To; recipients += message.Cc; recipients += message.Bcc; for(int i=0;i<recipients.Count;i++) { if (ActiveUp.Net.Mail.Validator.ValidateSyntax(recipients[i].Email)) { domains.SetValue(recipients[i].Email.Split('@')[1],i); adds.SetValue(recipients[i],i); } } System.Array.Sort(domains,adds,null); string currentDomain = ""; string address = ""; string buf = ""; ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); for(int j=0;j<adds.Length;j++) { address = ((ActiveUp.Net.Mail.Address)adds.GetValue(j)).Email; if(((string)domains.GetValue(j))==currentDomain) { smtp.RcptTo(address); if(j==(adds.Length-1)) { smtp.Data(message.ToMimeString(true));//,(message.Charset!=null ? message.Charset : "iso-8859-1")); smtp.Disconnect(); } } else { if(currentDomain!="") { smtp.Data(message.ToMimeString(true));//,(message.Charset!=null ? message.Charset : "iso-8859-1")); smtp.Disconnect(); smtp = new ActiveUp.Net.Mail.SmtpClient(); } currentDomain = (string)domains.GetValue(j); buf += currentDomain+"|"; if (dnsServers == null || dnsServers.Count == 0) { if (dnsServers == null) dnsServers = new ServerCollection(); IList<IPAddress> machineDnsServers = DnsQuery.GetMachineDnsServers(); foreach (IPAddress ipAddress in machineDnsServers) dnsServers.Add(ipAddress.ToString()); } ActiveUp.Net.Mail.MxRecordCollection mxs = ActiveUp.Net.Mail.Validator.GetMxRecords(currentDomain, dnsServers); if(mxs != null && mxs.Count>0) smtp.Connect(mxs.GetPrefered().Exchange); else throw new ActiveUp.Net.Mail.SmtpException("No MX record found for the domain \""+currentDomain+"\". Check that the domain is correct and exists or specify a DNS server."); try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } smtp.MailFrom(email); smtp.RcptTo(address); if (j == (adds.Length - 1)) { smtp.Data(message.ToMimeString(true));//,(message.Charset!=null ? message.Charset : "iso-8859-1")); smtp.Disconnect(); } } //} //catch(ActiveUp.Net.Mail.SmtpException ex) { throw ex; } } return buf; }
/// <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); }
public static IAsyncResult BeginGetInvalidAddresses(AddressCollection addresses, ServerCollection dnsServers, AsyncCallback callback) { _delegateGetInvalidAddressesServers = GetInvalidAddresses; return(_delegateGetInvalidAddressesServers.BeginInvoke(addresses, dnsServers, callback, _delegateGetInvalidAddressesServers)); }
public static IAsyncResult BeginFilter(AddressCollection addresses, AsyncCallback callback) { _delegateFilter = Filter; return(_delegateFilter.BeginInvoke(addresses, callback, _delegateFilter)); }
public static IAsyncResult BeginFilter(AddressCollection addresses, AsyncCallback callback) { SmtpValidator._delegateFilter = SmtpValidator.Filter; return(SmtpValidator._delegateFilter.BeginInvoke(addresses, callback, SmtpValidator._delegateFilter)); }
/// <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; }