email component

ANSMTP Developers Center > DNS Lookup

Send Email without SMTP Server (DNS Lookup) in C#

Important notice:* This product has integrated to EASendMail SMTP Component, Please use EASendMail SMTP Component instead of ANSMTP SMTP Component.

Related Links:
Send Email in C# with EASendMail SMTP Component

Introduction

This tutorial shows the algorithm of DNS lookup in ANSMTP which enables your app to send email without SMTP server.

Installation and Deployment

You should download the ansmtp installer and install it on your machine at first. If you want to distribute or deploy ansmtp without ansmtp installer, please click here to learn more.

How does it work?

In general, we send email via specified SMTP server. How does the specified SMTP server know what address this email should be sent to? It queries MX record of recipient's domain via DNS lookup. Then it forwards this email to the SMTP server queried from DNS server. If the recipient's server doesn't work fine, your SMTP server will send an email to tell the sender there is failure in sending this email.

How does ANSMTP work with DNS lookup? ANSMTP queries DNS directly from DNS, then send email to recipient's email server. If it fail, SendMail method will fail in synchronous mode and OnError event will occur (instead of sending an email to tell sender) in asynchronous mode, and your application can do something for this failure. If it fail in sending email via DNS lookup, it will also fail in sending email with specified SMTP server.

Send email without specified SMTP server

We just need to assign "" to ServerAddr property of ANSMTP, ANSMTP would send email via automatically.

[C# Example]
private void SendEmail()
{
  AOSMTPLib.MailClass oSmtp = new AOSMTPLib.MailClass();
  //If you don't have a SMTP server, use the following code
  //send email via DNS lookup, ANSMTP lookups SMTP server automatically.
  oSmtp.ServerAddr = "";  
  
  oSmtp.FromAddr = "test@adminsystem.net";
  oSmtp.AddRecipient( "Support Team", "support@adminsystem.net", 0 );

  oSmtp.Subject = "Test";
  oSmtp.BodyText = "Hello, this is a test....";
  
  if( oSmtp.SendMail() == 0 )
    Console.WriteLine( "Message delivered!" );
  else
    Console.WriteLine( oSmtp.GetLastErrDescription());
}

Limitation with DNS lookup

If you specified multiple recipients,  make sure all recipients' email addresses belong to the same domain, otherwise "Error with sending recipient" occurs.

[C# Example]
private void SendEmail()
{
  AOSMTPLib.MailClass oSmtp = new AOSMTPLib.MailClass();
  //If you don't have a SMTP server, use the following code
  //send email via DNS lookup, ANSMTP lookups SMTP server automatically.
  oSmtp.ServerAddr = "";  
  
  oSmtp.FromAddr = "test@emailarchitect.net";
  'Correct, both support@adminsystem.net and test@adminsystem.net
  'belong to adminsystem.net
  oSmtp.AddRecipient( "Support Team", "support@adminsystem.net", 0 );
  oSmtp.AddRecipient( "Tester", "test@adminsystem.net", 1 ); 
  
  'Incorrect, support@adminsystem.net and test@hotmail.com don't belong
  'to the same domain.
  'oSmtp.AddRecipient( "Support Team", "support@adminsystem.net", 0 );
  'oSmtp.AddRecipient( "Tester", "test@hotmail.com", 1 );
  
  oSmtp.Subject = "Test";
  oSmtp.BodyText = "Hello, this is a test....";
  
  if( oSmtp.SendMail() == 0 )
    Console.WriteLine( "Message delivered!" );
  else
    Console.WriteLine( oSmtp.GetLastErrDescription());
}

The code below shows how to send email to multiple recipients who don't belong to the same domain.

[C# Example]
private void SendEmail()
{
  AOSMTPLib.MailClass oSmtp = new AOSMTPLib.MailClass();
  oSmtp.FromAddr = "test@emailarchitect.net";
  oSmtp.Subject = "Test";
  oSmtp.BodyText = "Hello, this is a test....";
  
  string[] recipients = new string[2];
  recipients[0] = "support@adminsystem.net";
  recipients[1] = "test@hotmail.com";
  
  for( int i = 0; i < recipients.Length; i++ )
  {
    oSmtp.ClearRecipient();
    oSmtp.AddRecipient( recipients[i], recipients[i], 0 );
    oSmtp.ServerAddr = "";     
    if( oSmtp.SendMail() == 0 )
      Console.WriteLine( "Message to {0} delivered!", recipients[i] );
    else
      Console.WriteLine( "Message to {0} failed: {1}", 
                        recipients[i], 
                        oSmtp.GetLastErrDescription());
  }
}

Note*: In asynchronous mode, you can also specify ServerAddr as "" to send email via DNS lookup.

Free Email Support

Not enough? Please contact our technical support team.

Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered User)

Remarks
We usually reply emails in 24hours. The reason for getting no response is likely that your SMTP server bounced our reply. In this case, please try to use another email address to contact us. Your Hotmail or Yahoo email account is recommended.



2001-2011 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map                       Follow emailarchitect on Twitter