How to verify email address using mx record?
-
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
-
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
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
-
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
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 theIsValidEmailDomain
, 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
-
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 theIsValidEmailDomain
, 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
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.comYou 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