Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to verify email address using mx record?

How to verify email address using mx record?

Scheduled Pinned Locked Moved ASP.NET
questioncomhelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    miss786
    wrote on last edited by
    #1

    I am writing to seek help, in regards to figuring out, how can I verify my email address using mx record. I am currently new into using MX records and so far, I have method which returns a mx record for specific domain (as shown below):

    public static string mailMX()
    {
    string domainName = "#######.com";
    string mail = "";

        var response = DnsClient.Default.Resolve(domainName , RecordType.Mx);
        var records = response.AnswerRecords.OfType();
        foreach (var record in records)
        {
           return (record.ExchangeDomainName).ToString();
        }
    
        return mail = records.ToString();
    
    }
    

    My desire goal is to create a method, which can verify the email address "info@reply.####.com" has a valid mx record and return the email address. I am slightly struggling in this area, as I am not to sure how to structure this task. Any hints would be most appreciated. Thank you

    Richard DeemingR 1 Reply Last reply
    0
    • M miss786

      I am writing to seek help, in regards to figuring out, how can I verify my email address using mx record. I am currently new into using MX records and so far, I have method which returns a mx record for specific domain (as shown below):

      public static string mailMX()
      {
      string domainName = "#######.com";
      string mail = "";

          var response = DnsClient.Default.Resolve(domainName , RecordType.Mx);
          var records = response.AnswerRecords.OfType();
          foreach (var record in records)
          {
             return (record.ExchangeDomainName).ToString();
          }
      
          return mail = records.ToString();
      
      }
      

      My desire goal is to create a method, which can verify the email address "info@reply.####.com" has a valid mx record and return the email address. I am slightly struggling in this area, as I am not to sure how to structure this task. Any hints would be most appreciated. Thank you

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Ignore the ExchangeDomainName - that's just the name of the server which accepts email for the specified domain. If there are any MX records for the domain, then the domain is valid. It won't tell you whether the mailbox actually exists, but you can't determine that without trying to send an email. Try something like this:

      public static bool IsValidEmailDomain(MailAddress address)
      {
      if (address == null) return false;

      // TODO: Check what this method does with an invalid or unknown domain.
      // If it throws an exception, you'll need a try..catch block.
      var response = DnsClient.Default.Resolve(address.Host, RecordType.Mx);
      if (response == null || response.AnswerRecords == null) return false;
      
      return response.AnswerRecords.OfType<MxRecord>().Any();
      

      }

      public static bool IsValidEmailDomain(string address)
      {
      if (string.IsNullOrWhiteSpace(address)) return false;

      MailAddress theAddress;
      try
      {
          theAddress = new MailAddress(address);
      }
      catch (FormatException)
      {
          return false;
      }
      
      return IsValidEmailDomain(theAddress);
      

      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Ignore the ExchangeDomainName - that's just the name of the server which accepts email for the specified domain. If there are any MX records for the domain, then the domain is valid. It won't tell you whether the mailbox actually exists, but you can't determine that without trying to send an email. Try something like this:

        public static bool IsValidEmailDomain(MailAddress address)
        {
        if (address == null) return false;

        // TODO: Check what this method does with an invalid or unknown domain.
        // If it throws an exception, you'll need a try..catch block.
        var response = DnsClient.Default.Resolve(address.Host, RecordType.Mx);
        if (response == null || response.AnswerRecords == null) return false;
        
        return response.AnswerRecords.OfType<MxRecord>().Any();
        

        }

        public static bool IsValidEmailDomain(string address)
        {
        if (string.IsNullOrWhiteSpace(address)) return false;

        MailAddress theAddress;
        try
        {
            theAddress = new MailAddress(address);
        }
        catch (FormatException)
        {
            return false;
        }
        
        return IsValidEmailDomain(theAddress);
        

        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        miss786
        wrote on last edited by
        #3

        Thank you very much for your reply. Apology for the late response. When I tested the IsValidEmailDomain method, for my domain, it showed up as false. I tried adding in the try...catch block, but I am still getting a false domain address (####.com) and I have also tried testing it using my personal domain address (####@gmail.com), which also results in false. I have tried compiling many variations of the test method, to catch the error within the IsValidEmailDomain, but I keep experiencing false/null for each of the domain address.

            public static bool IsValidEmailDomain(MailAddress address)
            {
                if (address == null) return false;
                var response = DnsClient.Default.Resolve(address.Host, RecordType.Mx);
                try
                {
                    if (response == null || response.AnswerRecords == null) return false;
                }
                catch (FormatException)
                {
                    return false;
                }
        
                return response.AnswerRecords.OfType().Any();
            }
        
         public static string test()
            {
                string mail = "#####6@gmail.com";
                bool? answer = null;
                Exception ex;
                try
                {
                    answer = IsValidEmailDomain(mail);
                }
                catch (Exception e)
                {
                    ex = e;
                }
                if (answer.HasValue)
                {
                    return answer.ToString();
                }
                else
                {
                    return null;
                    // catch exception
                }
            }
        

        However, when I test the following domain address (###.com) with my original mx() method, it displays a mx-record (mail.###.com) for my domain address. I have also tested my mx() method using my gmail address and it outputs an mx-record for it.

         public static string mailMX()
            {
                string domainName = "#######.com";
                string mail = "";
        
                var response = DnsClient.Default.Resolve(domainName, RecordType.Mx);
                var records = response.AnswerRecords.OfType();
               foreach (var record in records)
                {
                   return (record.ExchangeDomainName).ToString();
                }
        
               return mail = records.ToString();
            }
        

        I am trying to send email via XML to 3rd party client via web request and in order to send the e

        Richard DeemingR 1 Reply Last reply
        0
        • M miss786

          Thank you very much for your reply. Apology for the late response. When I tested the IsValidEmailDomain method, for my domain, it showed up as false. I tried adding in the try...catch block, but I am still getting a false domain address (####.com) and I have also tried testing it using my personal domain address (####@gmail.com), which also results in false. I have tried compiling many variations of the test method, to catch the error within the IsValidEmailDomain, but I keep experiencing false/null for each of the domain address.

              public static bool IsValidEmailDomain(MailAddress address)
              {
                  if (address == null) return false;
                  var response = DnsClient.Default.Resolve(address.Host, RecordType.Mx);
                  try
                  {
                      if (response == null || response.AnswerRecords == null) return false;
                  }
                  catch (FormatException)
                  {
                      return false;
                  }
          
                  return response.AnswerRecords.OfType().Any();
              }
          
           public static string test()
              {
                  string mail = "#####6@gmail.com";
                  bool? answer = null;
                  Exception ex;
                  try
                  {
                      answer = IsValidEmailDomain(mail);
                  }
                  catch (Exception e)
                  {
                      ex = e;
                  }
                  if (answer.HasValue)
                  {
                      return answer.ToString();
                  }
                  else
                  {
                      return null;
                      // catch exception
                  }
              }
          

          However, when I test the following domain address (###.com) with my original mx() method, it displays a mx-record (mail.###.com) for my domain address. I have also tested my mx() method using my gmail address and it outputs an mx-record for it.

           public static string mailMX()
              {
                  string domainName = "#######.com";
                  string mail = "";
          
                  var response = DnsClient.Default.Resolve(domainName, RecordType.Mx);
                  var records = response.AnswerRecords.OfType();
                 foreach (var record in records)
                  {
                     return (record.ExchangeDomainName).ToString();
                  }
          
                 return mail = records.ToString();
              }
          

          I am trying to send email via XML to 3rd party client via web request and in order to send the e

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          I'm assuming the DnsClient class comes from the ArSoft.Tools.Net[^] project? Based on their documentation, I've made one slight adjustment to the methods I previously posted:

          public static bool IsValidEmailDomain(MailAddress address)
          {
          if (address == null) return false;

          var response = DnsClient.Default.Resolve(address.Host, RecordType.Mx);
          
          if (response == null) return false;
          if (response.ReturnCode != ReturnCode.NoError && response.ReturnCode != ReturnCode.NxDomain) return false;
          if (response.AnswerRecords == null) return false;
          
          return response.AnswerRecords.OfType<MxRecord>().Any();
          

          }

          public static bool IsValidEmailDomain(string address)
          {
          if (string.IsNullOrWhiteSpace(address)) return false;

          MailAddress theAddress;
          try
          {
              theAddress = new MailAddress(address);
          }
          catch (FormatException)
          {
              return false;
          }
          
          return IsValidEmailDomain(theAddress);
          

          }

          Testing those methods using v1.8.1 of the library produces the expected results:

          IsValidEmailDomain("test@gmail.com") == true;
          IsValidEmailDomain("test@test.test") == false;

          If those methods don't product true for a gmail.com address, then you need to check your DNS servers. Open a command prompt and type the following:

          nslookup
          set type=mx
          gmail.com

          You should see a list of the MX records for the domain.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups