How to Send Email Messages using DNS inside C# & VB.NET Apps

This technical tip explains how to send an email messages using DNS inside .NET applications. Sometimes sending mails using an SMTP server is not feasible for a project’s requirement. We may want to utilize the MX record of the recipient’s domain name. Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. Following is a quick sample that shows how to send mails using mail servers of recipient’s domain.
//your code here...//The sample code below sends email messages using DNS. //[C# Code Sample] static void Main(string[] args) { try { MailMessage msg = new MailMessage("add1@domain.com", "add1@domain.com", "test", "this is a test"); msg.CC.Add(new MailAddress("add2@domain.com", "CC Display Name")); DnsSendMessage(msg); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("Program finished. Press any key to exit...."); Console.ReadKey(); } /// <summary> /// Use for sent message /// </summary> /// <param name="msg">message to send</param> /// <returns></returns> private static void DnsSendMessage(MailMessage msg) { // Get all the recipients in to, cc and bcc in one collection MailAddressCollection addresses = new MailAddressCollection(); foreach (MailAddress to in msg.To) { addresses.Add(to); } foreach (MailAddress cc in msg.CC) { addresses.Add(cc); } foreach (MailAddress bcc in msg.Bcc) { addresses.Add(bcc); } // send mail using DNS to each address foreach (MailAddress addr in addresses) { //Find mail exchange servers with the help of DnsClient DnsClient dnsClient = new DnsClient(); Question mxQuestion = new Question(addr.Host, QueryType.MX); if (dnsClient.Resolve(mxQuestion)) { // try to send a message foreach (ResourceRecord record in dnsClient.ReceivedMessage.Answers) { MXResourceRecord cnRecord = record as MXResourceRecord; if (cnRecord != null) { try { // Send message SmtpClient client = new SmtpClient(); client.AuthenticationMethod = SmtpAuthentication.None; client.Host = cnRecord.ExchangeName; client.Port = 25; client.Send(msg); Console.WriteLine("Mail sent to " + addr.Address); } catch (SmtpException e) { Console.WriteLine(cnRecord.ExchangeName + ": " + e.Message + Environment.NewLine); continue; } } } } } } //[VB.NET code Sample] Sub Main(ByVal args As String()) Try Dim msg As MailMessage = New MailMessage("add1@domain.com", "add1@domain.com", "test", "this is a test") msg.CC.Add(New MailAddress("add2@domain.com", "CC Display Name")) DnsSendMessage(msg) Catch e As Exception Console.WriteLine(e.Message) End Try Console.WriteLine("Program finished. Press any key to exit....") Console.ReadKey() End Sub ''' <summary> ''' Use for sent message ''' </summary> ''' <param name="msg">message to send</param> ''' <returns></returns> Private Sub DnsSendMessage(ByVal msg As MailMessage) ' Get all the recipients in to, cc and bcc in one collection Dim addresses As MailAddressCollection = New MailAddressCollection() For Each [to] As MailAddress In msg.To addresses.Add([to]) Next to For Each cc As MailAddress In msg.CC addresses.Add(cc) Next cc For Each bcc As MailAddress In msg.Bcc addresses.Add(bcc) Next bcc ' send mail using DNS to each address For Each addr As MailAddress In addresses 'Find mail exchange servers with the help of DnsClient Dim dnsClient As DnsClient = New DnsClient() Dim mxQuestion As Question = New Question(addr.Host, QueryType.MX) If dnsClient.Resolve(mxQuestion) Then ' try to send a message For Each record As ResourceRecord In dnsClient.ReceivedMessage.Answers Dim cnRecord As MXResourceRecord = TryCast(record, MXResourceRecord) If Not cnRecord Is Nothing Then Try ' Send message Dim client As SmtpClient = New SmtpClient() client.AuthenticationMethod = SmtpAuthentication.None client.Host = cnRecord.ExchangeName client.Port = 25 client.Send(msg) Console.WriteLine("Mail sent to " & addr.Address) Catch e As SmtpException Console.WriteLine(cnRecord.ExchangeName & ": " & e.Message & Environment.NewLine) Continue For End Try End If Next record End If Next addr End Sub

Url: http://www.aspose.com/.net/email-component.aspx

Language: C# | User: Sheraz Khan | Created: Nov 11, 2015 | Tags: send email messages using DNS Send Email Client inside .NET Apps component emails SMTP server mail to multiple address