Author Posts

October 21, 2015 at 10:33 am

Hi,

I have found this function to validate some mail addresses from a list>


function Validate-Mail{
Param ([String]$Address)
($Address -as [System.Net.Mail.MailAddress]).Address `
-eq $Address -and $Address -ne $null}

But it only works for mail addresses like this:
test@@aol.com
test.gmail.com

And I want to add this kind of mail address in the block list:
informações@uol.com.br

Since "ç" and "õ" aren't used on mail addresses.
Need to find a way to block any address with graphic accentuations.

October 21, 2015 at 10:36 am

you will need to use a regular expression (Regex) let me do some looking for you.

if($email -match "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"){"true"}
else{"False"}

http://www.regular-expressions.info/email.html

October 21, 2015 at 11:33 am

Maybe this

function Validate-Mail{
Param(

       [ValidatePattern(""^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$")]

       ($Address -as [System.Net.Mail.MailAddress]).Address `
-eq $Address -and $Address -ne $null}

 )

October 22, 2015 at 2:53 am

Mark Hammonds,

Thanks! The first one worked perfectly =]


if($email -match "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"){"true"}
else{"False"}

October 22, 2015 at 3:23 am

the long version version of RFC 1035 is :

\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*
| "(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]
| \\[\x01-\x09\x0b\x0c\x0e-\x7f])*")
@ (?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
| \[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:
(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]
| \\[\x01-\x09\x0b\x0c\x0e-\x7f])+)
\])\z

"scream" !

but the short :

\A[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@
(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z

or above works in 99% of cases