Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Exchange SMTP Splitting at the "@"

$
0
0

It’s probably not necessary in this case, since Exchange should always be giving you well-formatted email addresses, but if you were accepting some string input that needs validation (and separation), I’d recommend just using the .NET System.Net.Mail.MailAddress class. If you try to cast a string to that type and it’s formatted wrong (multiple @ symbols, illegal characters, etc), you’ll get an exception. Once you have the object, it already provides “User” and “Host” properties you can read:

$maybeValidEmailAddresses = @('user@domain.com','bogus@string@invalid.com')

foreach ($string in $maybeValidEmailAddresses) {
    try {
        $addr = [System.Net.Mail.MailAddress]$string
        Write-Host "User: $($addr.User), Host: $($addr.Host)"
    } catch {
        # Handle the error however you'd like.
        Write-Error -ErrorRecord $_
    }
}

Viewing all articles
Browse latest Browse all 13067

Trending Articles