/// <summary> /// Validates syntax and existence of the given address. /// </summary> /// <param name="address">The address to be validated.</param> /// <param name="dnsServers">Name Servers to be used for MX records search.</param> /// <returns>True if the address is valid, otherwise false.</returns> public static bool Validate(string address, ServerCollection dnsServers) { if (!ActiveUp.Net.Mail.Validator.ValidateSyntax(address)) { return(false); } else { string domain = address.Split('@')[1]; bool result; ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.SendTimeout = 15; smtp.ReceiveTimeout = 15; MxRecordCollection mxRecords = new MxRecordCollection(); try { #if !PocketPC mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain, dnsServers); #else mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain); #endif } catch { new System.Exception("Can't connect to DNS server."); } smtp.Connect(mxRecords.GetPrefered().Exchange); try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } if (smtp.Verify(address)) { result = true; } else { try { //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName()); //smtp.MailFrom("*****@*****.**"); smtp.MailFrom(string.Format("postmaster@{0}", domain)); smtp.RcptTo(address); result = true; } catch { result = false; } } smtp.Disconnect(); return(result); } }
/// <summary> /// Sends the message using the specified host and port after authentication. /// </summary> /// <param name="messages">The message collection to be sent.</param> /// <param name="host">Host to be used to send the message.</param> /// <param name="username">Username to be used for the authentication process.</param> /// <param name="password">Password to be used for the authentication process.</param> /// <param name="mechanism">SASL mechanism to be used.</param> /// <param name="port">Port to be used to connect to the specified host.</param> /// <param name="errors">Reference to SmtpException object collection where errors occuring during the process will be stored.</param> /// <returns>Amount of messages successfully sent.</returns> public static int SendCollection(MessageCollection messages, string host, int port, string username, string password, SaslMechanism mechanism, ref SmtpExceptionCollection errors) { ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.Connect(host,port); try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } smtp.Authenticate(username,password,mechanism); int sent = 0; foreach(Message message in messages) { try { // Ensure that the mime part tree is built message.CheckBuiltMimePartTree(); if(message.From.Email!=string.Empty) smtp.MailFrom(message.From); else smtp.MailFrom(message.Sender); smtp.RcptTo(message.To); smtp.RcptTo(message.Cc); smtp.RcptTo(message.Bcc); smtp.Data(message.ToMimeString()); sent++; } catch(ActiveUp.Net.Mail.SmtpException ex) { errors.Add(ex); } } smtp.Disconnect(); return sent; }
/// <summary> /// Sends all messages using the specified host and port. /// </summary> /// <param name="messages">The message collection to be sent.</param> /// <param name="host">Address of the server to be used.</param> /// <param name="port">Port to be used.</param> /// <returns>Amount of messages successfully sent.</returns> /// <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!"; /// /// Message message1 = new Message(); /// message1.Subject = "Hey David!"; /// message1.From = New Address("*****@*****.**","John Doe"); /// message1.To.Add("*****@*****.**","David Clark"); /// message1.BodyText.Text = "How you doing ?"; /// /// MessageCollection messages = new MessageCollection(); /// messages.Add(message); /// messages.Add(message1); /// /// SmtpClient.Send(messages,"mail.myhost.com",8504); /// /// 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 message1 As New Message /// message1.Subject = "Hey David!" /// message1.From = New Address("*****@*****.**","John Doe") /// message1.To.Add("*****@*****.**","David Clark") /// message1.BodyText.Text = "How you doing ?" /// /// Dim messages As New MessageCollection /// messages.Add(message) /// messages.Add(message1) /// /// SmtpClient.Send(messages,"mail.myhost.com",8504) /// /// 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 message1:Message = new Message(); /// message1.Subject = "Hey David!"; /// message1.From = New Address("*****@*****.**","John Doe"); /// message1.To.Add("*****@*****.**","David Clark"); /// message1.BodyText.Text = "How you doing ?"; /// /// var messages:MessageCollection = new MessageCollection(); /// messages.Add(message); /// messages.Add(message1); /// /// SmtpClient.Send(messages,"mail.myhost.com",8504); /// </code> /// </example> public static int SendCollection(MessageCollection messages, string host, int port) { ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.Connect(host,port); try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } int sent=0; foreach(Message message in messages) { try { // Ensure that the mime part tree is built message.CheckBuiltMimePartTree(); if(message.From.Email!=string.Empty) smtp.MailFrom(message.From); else smtp.MailFrom(message.Sender); smtp.RcptTo(message.To); smtp.RcptTo(message.Cc); smtp.RcptTo(message.Bcc); smtp.Data(message.ToMimeString()); sent++; } catch(ActiveUp.Net.Mail.SmtpException) { } } smtp.Disconnect(); return sent; }
/// <summary> /// Sends all messages using the specified host. /// </summary> /// <param name="servers">Servers to be used to send the message (in preference order).</param> /// <param name="messages">MessageCollection to be sent.</param> /// <param name="errors">Reference to SmtpException object collection where errors occuring during the process will be stored.</param> /// <returns>Amount of messages successfully sent.</returns> /// <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!"; /// /// Message message1 = new Message(); /// message1.Subject = "Hey David!"; /// message1.From = New Address("*****@*****.**","John Doe"); /// message1.To.Add("*****@*****.**","David Clark"); /// message1.BodyText.Text = "How you doing ?"; /// /// MessageCollection messages = new MessageCollection(); /// messages.Add(message); /// messages.Add(message1); /// /// ServerCollection servers = new ServerCollection(); /// servers.Add("mail.myhost.com",25); /// servers.Add("mail2.myhost.com",25); /// /// SmtpClient.Send(messages,servers,myErrorCollection); /// /// 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 message1 As New Message /// message1.Subject = "Hey David!" /// message1.From = New Address("*****@*****.**","John Doe") /// message1.To.Add("*****@*****.**","David Clark") /// message1.BodyText.Text = "How you doing ?" /// /// Dim messages As New MessageCollection /// messages.Add(message) /// messages.Add(message1) /// /// Dim servers As New ServerCollection /// servers.Add("mail.myhost.com",25) /// servers.Add("mail2.myhost.com",25) /// /// SmtpClient.Send(messages,servers,myErrorCollection) /// /// 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 message1:Message = new Message(); /// message1.Subject = "Hey David!"; /// message1.From = New Address("*****@*****.**","John Doe"); /// message1.To.Add("*****@*****.**","David Clark"); /// message1.BodyText.Text = "How you doing ?"; /// /// var messages:MessageCollection = new MessageCollection(); /// messages.Add(message); /// messages.Add(message1); /// /// var servers:ServerCollection = new ServerCollection(); /// servers.Add("mail.myhost.com",25); /// servers.Add("mail2.myhost.com",25); /// /// SmtpClient.Send(messages,servers,myErrorCollection); /// </code> /// </example> public static int SendCollection(MessageCollection messages, ServerCollection servers, ref SmtpExceptionCollection errors) { ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); int sent=0; foreach(Message message in messages) { try { // Ensure that the mime part tree is built message.CheckBuiltMimePartTree(); for(int i=0;i<servers.Count;i++) { try { if (servers[i].ServerEncryptionType != EncryptionType.None) { #if !PocketPC smtp.ConnectSsl(servers[i].Host, servers[i].Port); #else smtp.Connect(servers[i].Host, servers[i].Port); #endif } else { smtp.Connect(servers[i].Host, servers[i].Port); } try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } if(servers[i].Username!=null && servers[i].Username.Length>0 && servers[i].Password!=null && servers[i].Password.Length>0) smtp.Authenticate(servers[i].Username,servers[i].Password,SaslMechanism.Login); if(message.From.Email!=string.Empty) smtp.MailFrom(message.From); else smtp.MailFrom(message.Sender); smtp.RcptTo(message.To); smtp.RcptTo(message.Cc); smtp.RcptTo(message.Bcc); smtp.Data(message.ToMimeString()); smtp.Disconnect(); sent++; break; } catch { continue; } } } catch(ActiveUp.Net.Mail.SmtpException ex) { errors.Add(ex); } } return sent; }
/// <summary> /// Sends the message using the specified host on the specified port. Secure SASL Authentication is performed according to the requested mechanism. /// </summary> /// <param name="message">The message to be sent.</param> /// <param name="host">The host to be used.</param> /// <param name="username">The username to be used for authentication.</param> /// <param name="password">The password to be used for authentication.</param> /// <param name="mechanism">SASL Mechanism to be used for authentication.</param> /// <param name="port">The port to be used.</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!"; /// /// SmtpClient.Send(message,"mail.myhost.com","jdoe1234","tanstaaf",SaslMechanism.CramMd5,8504); /// /// 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!" /// /// SmtpClient.Send(message,"mail.myhost.com","jdoe1234","tanstaaf",SaslMechanism.CramMd5,8504) /// /// 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!"; /// /// SmtpClient.Send(message,"mail.myhost.com","jdoe1234","tanstaaf",SaslMechanism.CramMd5,8504); /// </code> /// </example> public static bool Send(Message message, string host, int port, string username, string password, SaslMechanism mechanism) { // Ensure that the mime part tree is built message.CheckBuiltMimePartTree(); ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.Connect(host,port); try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } smtp.Authenticate(username,password,mechanism); if(message.From.Email!=string.Empty) smtp.MailFrom(message.From); else smtp.MailFrom(message.Sender); smtp.RcptTo(message.To); smtp.RcptTo(message.Cc); smtp.RcptTo(message.Bcc); smtp.Data(message.ToMimeString()); smtp.Disconnect(); return true; }
public static bool SendSsl(Message message, string server, int port) { // Ensure that the mime part tree is built message.CheckBuiltMimePartTree(); ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.ConnectSsl(server, port); try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } if (message.From.Email != string.Empty) smtp.MailFrom(message.From); else smtp.MailFrom(message.Sender); smtp.RcptTo(message.To); smtp.RcptTo(message.Cc); smtp.RcptTo(message.Bcc); smtp.Data(message.ToMimeString());//,(message.Charset!=null ? message.Charset : "iso-8859-1")); smtp.Disconnect(); return true; }
/// <summary> /// Sends the message using the specified host as mail exchange server. /// </summary> /// <param name="message">The message to be sent.</param> /// <param name="servers">Servers to be used to send the message (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("mail.myhost.com",25); /// servers.Add("mail2.myhost.com",25); /// /// SmtpClient.Send(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("mail.myhost.com",25) /// servers.Add("mail2.myhost.com",25) /// /// SmtpClient.Send(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("mail.myhost.com",25); /// servers.Add("mail2.myhost.com",25); /// /// SmtpClient.Send(message,servers); /// </code> /// </example> public static bool Send(Message message, ServerCollection servers, out string serverMessage) { // Ensure that the mime part tree is built message.CheckBuiltMimePartTree(); serverMessage = string.Empty; ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); bool messageSent = false; for(int i=0;i<servers.Count;i++) { try { if (servers[i].ServerEncryptionType != EncryptionType.None) { #if !PocketPC smtp.ConnectSsl(servers[i].Host,servers[i].Port); #else smtp.Connect(servers[i].Host, servers[i].Port); #endif }else { smtp.Connect(servers[i].Host,servers[i].Port); } try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } if(servers[i].Username!=null && servers[i].Username.Length>0 && servers[i].Password!=null && servers[i].Password.Length>0) smtp.Authenticate(servers[i].Username,servers[i].Password,SaslMechanism.Login); if(message.From.Email!=string.Empty) smtp.MailFrom(message.From); else smtp.MailFrom(message.Sender); smtp.RcptTo(message.To); smtp.RcptTo(message.Cc); smtp.RcptTo(message.Bcc); serverMessage = smtp.Data(message.ToMimeString());//,(message.Charset!=null ? message.Charset : "iso-8859-1")); smtp.Disconnect(); messageSent = true; break; } catch { continue; } } return messageSent; }
/// <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> /// 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; }
/// <summary> /// Validates syntax and existence of the given address. /// </summary> /// <param name="address">The address to be validated.</param> /// <returns>True if the address is valid, otherwise false.</returns> public static bool Validate(string address) { if (!ActiveUp.Net.Mail.Validator.ValidateSyntax(address)) return false; else { try { string domain = address.Split('@')[1]; bool result; ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.SendTimeout = 0; smtp.ReceiveTimeout = 0; MxRecordCollection mxRecords = new MxRecordCollection(); try { mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain); } catch { new System.Exception("Can't connect to DNS server."); } //Console.WriteLine(mxRecords.GetPrefered().Exchange); if (mxRecords.Count > 0) smtp.Connect(mxRecords.GetPrefered().Exchange); else return false; try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } if (smtp.Verify(address)) result = true; else { try { //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName()); //smtp.MailFrom("*****@*****.**"); smtp.MailFrom("postmaster@" + domain); smtp.RcptTo(address); result = true; } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); #if !PocketPC System.Web.HttpContext.Current.Trace.Write("ActiveMail", ex.ToString()); #endif result = false; } } smtp.Disconnect(); return result; } catch { return false; } } }
/// <summary> /// Validates syntax and existence of the given address. /// </summary> /// <param name="address">The address to be validated.</param> /// <param name="dnsServers">Name Servers to be used for MX records search.</param> /// <returns>True if the address is valid, otherwise false.</returns> public static bool Validate(string address, ServerCollection dnsServers) { if (!ActiveUp.Net.Mail.Validator.ValidateSyntax(address)) return false; else { string domain = address.Split('@')[1]; bool result; ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.SendTimeout = 15; smtp.ReceiveTimeout = 15; MxRecordCollection mxRecords = new MxRecordCollection(); try { #if !PocketPC mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain, dnsServers); #else mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain); #endif } catch { new System.Exception("Can't connect to DNS server."); } smtp.Connect(mxRecords.GetPrefered().Exchange); try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } if (smtp.Verify(address)) result = true; else { try { //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName()); //smtp.MailFrom("*****@*****.**"); smtp.MailFrom("postmaster@" + domain); smtp.RcptTo(address); result = true; } catch { result = false; } } smtp.Disconnect(); return result; } }
/// <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(string.Format("postmaster@{0}", 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); }
/// <summary> /// Validates syntax and existence of the given address. /// </summary> /// <param name="address">The address to be validated.</param> /// <returns>True if the address is valid, otherwise false.</returns> public static bool Validate(string address) { if (!ActiveUp.Net.Mail.Validator.ValidateSyntax(address)) { return(false); } else { try { string domain = address.Split('@')[1]; bool result; ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.SendTimeout = 0; smtp.ReceiveTimeout = 0; MxRecordCollection mxRecords = new MxRecordCollection(); try { mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain); } catch { new System.Exception("Can't connect to DNS server."); } //Console.WriteLine(mxRecords.GetPrefered().Exchange); if (mxRecords.Count > 0) { smtp.Connect(mxRecords.GetPrefered().Exchange); } else { return(false); } try { smtp.Ehlo(System.Net.Dns.GetHostName()); } catch { smtp.Helo(System.Net.Dns.GetHostName()); } if (smtp.Verify(address)) { result = true; } else { try { //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName()); //smtp.MailFrom("*****@*****.**"); smtp.MailFrom(string.Format("postmaster@{0}", domain)); smtp.RcptTo(address); result = true; } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); #if !PocketPC System.Web.HttpContext.Current.Trace.Write("ActiveMail", ex.ToString()); #endif result = false; } } smtp.Disconnect(); return(result); } catch { return(false); } } }
/// <summary> /// Sends the message using the specified host on the specified port. Secure SASL Authentication is performed according to the requested mechanism. /// </summary> /// <param name="message">The message to be sent.</param> /// <param name="host">The host to be used.</param> /// <param name="username">The username to be used for authentication.</param> /// <param name="password">The password to be used for authentication.</param> /// <param name="mechanism">SASL Mechanism to be used for authentication.</param> /// <param name="port">The port to be used.</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!"; /// /// SmtpClient.Send(message,"mail.myhost.com","jdoe1234","tanstaaf",SaslMechanism.CramMd5,8504); /// /// 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!" /// /// SmtpClient.Send(message,"mail.myhost.com","jdoe1234","tanstaaf",SaslMechanism.CramMd5,8504) /// /// 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!"; /// /// SmtpClient.Send(message,"mail.myhost.com","jdoe1234","tanstaaf",SaslMechanism.CramMd5,8504); /// </code> /// </example> public static bool Send(Message message, string host, int port, string username, string password, SaslMechanism mechanism) { // Ensure that the mime part tree is built message.CheckBuiltMimePartTree(); var smtp = new SmtpClient(); smtp.Connect(host,port); smtp.SendEhloHelo(); smtp.Authenticate(username, password, mechanism); if(message.From.Email!=string.Empty) smtp.MailFrom(message.From); else smtp.MailFrom(message.Sender); smtp.RcptTo(message.To); smtp.RcptTo(message.Cc); smtp.RcptTo(message.Bcc); smtp.Data(message.ToMimeString()); smtp.Disconnect(); return true; }
private static void SendMessageWithAuthentication(SmtpClient smtp, string username, string password, SaslMechanism mechanism, Message message) { smtp.Authenticate(username, password, mechanism); if (message.From.Email != string.Empty) smtp.MailFrom(message.From); else smtp.MailFrom(message.Sender); smtp.RcptTo(message.To); smtp.RcptTo(message.Cc); smtp.RcptTo(message.Bcc); smtp.Data(message.ToMimeString()); smtp.Disconnect(); }
private static void SendMessageWith(SmtpClient smtp, Message message) { if (message.From.Email != string.Empty) smtp.MailFrom(message.From); else smtp.MailFrom(message.Sender); smtp.RcptTo(message.To); smtp.RcptTo(message.Cc); smtp.RcptTo(message.Bcc); smtp.Data(message.ToMimeString());//,(message.Charset!=null ? message.Charset : "iso-8859-1")); smtp.Disconnect(); }