Check Valid email address in c#
Solution 1
You can import System.ComponentModel.DataAnnotations
and use it in this way:
private bool validMail(string address)
{
EmailAddressAttribute e = new EmailAddressAttribute();
if (e.IsValid(address))
return true;
else
return false;
}
Solution 2
You can use regular expressions like:
public static bool IsValidEmailAddress(string emailaddress)
{
try
{
Regex rx = new Regex(
@"^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$");
return rx.IsMatch(emailaddress);
}
catch (FormatException)
{
return false;
}
}
UPDATE:
If you want to validate for a specific domain as you said in comments its even simpler:
Regex rx = new Regex(
@"^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@yourdomain.com$");
Replace yourdomain.com with your domain name.
Solution 3
public static bool IsValidEmail(string emailaddress)
{
return new EmailAddressAttribute().IsValid(emailaddress);
}
// using System.ComponentModel.DataAnnotations;
Solution 4
Using the .Net mail namespace you can verify email address like this:
using System.Net.Mail;
private bool EmailVerify(string email)
{
try
{
var mail = new MailAddress(email);
return mail.Host.Contains(".");
}
catch
{
return false;
}
}
Sunil Acharya
I believe in making simple but useful apps that can ease people's life and can bring smile to their faces. Loves coding and checking out new technologies. There is always something out there to learn. Understand it, if useful implement asps.
Updated on August 03, 2020Comments
-
Sunil Acharya almost 4 years
I'm sending email using smtp services in my c# windows application. I have to perform in a best way to reduce the email bounce rate. I have to check the provided email address is valid or not. I'm using the code.
private void btnCheckValid_Click(object sender, EventArgs e) { if (isRealDomain(textBox1.Text.Trim()) == true) MessageBox.Show("Valid Email Address!"); } private bool isRealDomain(string inputEmail) { bool isReal = false; try { string[] host = (inputEmail.Split('@')); string hostname = host[1]; IPHostEntry IPhst = Dns.GetHostEntry(hostname); IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25); Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp); s.Connect(endPt); s.Close(); isReal = true; } catch (Exception ex) { MessageBox.Show(ex.Message); isReal = false; } return isReal; }
By checking the real domain I can identified the Hosted IP but the email address is created on the host or not.
with regex (regular expression ) I can valid only format.
So my question is how to find only valid address of any domain in c#.
-
Sunil Acharya about 8 yearsIf you can see I'm getting the IPHostEntry in my code but. having the host is didn't identify the real email address.
-
Kevin about 8 yearsCorrect, and there is essentially nothing you can do before sending the email that will guarantee an email address exists. All you can hope to do, is reduce bounces by ensuring you are only sending mail to addresses that are properly formatted. For example [email protected] is correctly formatted as an email address, but even if you can determine that mydomain.com is a valid active domain, you cannot determine if testaddress exists as a valid mail account in that domain.
-
Kevin about 8 yearsAlso, I'm pointing out in my answer, that Dns.GetHostEntry will sometimes fail to find perfectly valid active domains, particularly multi level domains so, in those cases, you will not get an IPHostEntry even though the domain exists. Your technique for determining a valid domain will catch most domains, but not all.
-
aruno about 7 yearsthis sounds great, but unfortunately it allows
example@example
which of course doesn't contain a TLD at all. This may be fine for an intranet, but for most people I'd recommend also adding the change I made to your answer -
aruno about 7 yearsyou can also add common typos like
mail.Host.EndsWith(".cmo")
and return false -
Sunil Acharya almost 6 yearsMy intensence is not to check the email format I wanted to valid the email address in real time...