How to validate email domain name?

Posted by Only4ram under ASP.NET on 11/26/2014 | Points: 10 | Views : 388 | Status : [Member] | Replies : 2
Dear Friends,

Can you please explain that how to validate email's domain name using c#.net?
For example :

If we have one email is "abc@gmail.com" here i need to validate gmail is valid domain or fake domain




Responses

Posted by: Sheonarayan on: 11/26/2014 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Only4ram,

Here is the article written by me that explains how to check whether a particular domain is valid or not http://www.dotnetfunda.com/articles/show/1072/validating-domain-name-in-aspnet.

Hope this helps.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Only4ram, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Punitha on: 12/4/2014 [Member] Starter | Points: 25

Up
0
Down
Hope this might work.

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class RegexUtilities
{
bool invalid = false;

public bool IsValidEmail(string strIn)
{
invalid = false;
if (String.IsNullOrEmpty(strIn))
return false;

// Use IdnMapping class to convert Unicode domain names.
try {
strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
RegexOptions.None, TimeSpan.FromMilliseconds(200));
}
catch (RegexMatchTimeoutException) {
return false;
}

if (invalid)
return false;

// Return true if strIn is in valid e-mail format.
try {
return Regex.IsMatch(strIn,
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
}
catch (RegexMatchTimeoutException) {
return false;
}
}

private string DomainMapper(Match match)
{
// IdnMapping class with default property values.
IdnMapping idn = new IdnMapping();

string domainName = match.Groups[2].Value;
try {
domainName = idn.GetAscii(domainName);
}
catch (ArgumentException) {
invalid = true;
}
return match.Groups[1].Value + domainName;
}
}


Only4ram, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response