Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MailAddress think 'john@gmail.' is a valid email address?

Like a good C# user, I use the MailAddress object to validate email addresses.

A client of mine entered john@gmail. for his email, which was validated by MailAddress, and broke my software. I'd expect the code below to throw an exception, but it doesn't.

static void Main(string[] args)
{
    string addressmail = string.Empty;

    try
    {
        MailAddress mail = new MailAddress(@"john@gmail.");
        addressmail = mail.Address;
    }
    catch (FormatException)
    {
        // address is invalid
    }

    // address is valid
    Console.WriteLine(addressmail);
}

Do you know how to catch this kind of bogus mail address?

like image 334
Vinzz Avatar asked Aug 24 '11 09:08

Vinzz


People also ask

Why does it keep saying enter a valid email address?

A valid email address holds the correct format and belongs to an email domain. Invalid email addresses belong to inactive recipients who don't engage with your content. The unengaged email addresses can be invalid for a few reasons: Invalid emails can have typos or misformatting that do not lead to a legitimate inbox.

Why does my Gmail address say invalid?

This can happen for any of the following reasons: your password has been entered incorrectly multiple times. your mail client is configured to check for emails too often (Google recommends only once every 10 minutes) your password isn't strong enough.

Why is it saying my email is invalid?

An invalid email occurs when you attempt to send email to an address that is formatted in a manner that does not meet internet email format standards or the email does not exist at the recipient's mail server.


1 Answers

I think in this case, MS's implementation of a valid email address is incorrect, at least as per RFC822. I haven't actually tried your code, so I'm assuming it does as you say.

There are other ways to validate email addresses, such as actually connecting to the SMTP server and asking it to confirm that the address is valid (as explained here and here). Short of doing that, you will always have a bit of trouble. Personally, I don't think that it's worthwhile to spend too much time validating email address according to some specification (beyond the quick checks we have at our disposal; e.g. your code) - the real test is whether an email is received on that address if you send it. A simple email verification can confirm this, although I know it might not be appropriate in all cases, but in those, you are out of luck.

like image 72
Daniel B Avatar answered Sep 18 '22 01:09

Daniel B