locked
Simple way to validate an email address RRS feed

  • General discussion

  • I know there are regex expressions to validate an email address, but I thought I'd like to use other folks works. So, to validate an email address, I use

    New-Object System.Net.Mail.MailAddress('greg@somewhere.com')
    

    When the format is bad, you get

    New-Object System.Net.Mail.MailAddress('gregsomewhere.com')
    New-Object : Exception calling ".ctor" with "1" argument(s): "The specified string is not in the form required for an e
    -mail address."
    At line:1 char:11
    + New-Object <<<< System.Net.Mail.MailAddress('gregsomewhere.com')
      + CategoryInfo     : InvalidOperation: (:) [New-Object], MethodInvocationException
      + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    

    Need to wrap this in a try/catch, but end up using the brains at Microsoft to validate.

    Thoughts?


    GregM
    Friday, July 8, 2011 1:47 PM

All replies

  • yeah would be nice if there was a static method to test address, but I like that method... really it comes down to speed.. is it faster to do this test or to a regex test? probably isnt going to matter on a normal PC..  the real problem is that since you cant nest a try/catch you have to do a lot of them

    try{
       $toadr = new-object net.mail.mailaddress($to)
    }
    catch
    { #bad to address}
    try{
       $fromadr = new-object net.mail.mailaddress($from)
    }
    catch
    { #bad from address}

    then really you should wrap the send in a try catch incase the smtp server goes down.... etc etc....

    Rule of coding: 20% logic, 80% error handling/checking :)

    but yeah, I like that method...


    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, July 8, 2011 2:01 PM
  • I made a function

    Function ValidEmailAddress($address)
    {
     try
     {
      $x = New-Object System.Net.Mail.MailAddress($address)
      return $true
     }
     catch
     {
      return $false
     }
    }
    


    GregM
    Friday, July 8, 2011 2:23 PM
  • [bool]($address -as [Net.Mail.MailAddress])
    Friday, July 8, 2011 2:31 PM
  • Ouch!
    GregM
    Friday, July 8, 2011 2:33 PM
  • $toadr = new-object net.mail.mailaddress($to)

    doesn't check if there is a .com etc at the end.

    Try settings $to = "test@testcom" and it WON'T throw an exception.

    They really need to fix this.

    Monday, February 17, 2014 12:14 AM
  • fix what? There doesn't need to be a .COM at the end of a valid SMTP address. My own work address ends in .CA

    imho, the only way to properly fix the domain part of the address would be to have a list of all valid domains to compare to. But, being dynamic, that would be as difficult to get as a list of all valid email addresses at a given domain.

    You could use a list of all valid top-level DC values, like .COM, .ORC, .CA (and the rest of the country-related domains), and expect to see at least one of them as the last "."-separated field in the domain.

    I guess what it comes down to is what is the purpose here. Perhaps it would be enough to match something like user@whatever.xxx. But, no matter how well the address is parsed, there is still no guarantee that a message delivered will get to where it is intended to go.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Monday, February 17, 2014 4:26 PM
  • [bool]($address -as [Net.Mail.MailAddress])
    Very nice.
    Tuesday, May 20, 2014 5:35 PM
  • [bool]($address -as [Net.Mail.MailAddress]) doesn't check for a .xxx so A@A evaluates true but isn't really going to work.

    Trying to evaluate top level domains from a list is going to cause problems for anyone who has an email that is from a domain not in the list.  I have a .to email and not so much anymore, but I used to run into issues where validation rejected it.  I'd say use regex with a good pattern - if the email address isn't entered correctly so the email never gets received, at least you've done your part to ensure it's in the right format so the send command works.


    I hope this post has helped!

    Tuesday, May 20, 2014 7:31 PM