I have a web page that we allow ou administrators to use to modify mailboxes. One task allows them to change an email address.
This comes in handy when a user gets married or divorced, or changes his or her name, and in those cases, usually we would like to keep the old email addresses so mail to those old email addresses still goes to the mailbox.
So, I created a script to add an email address, and set it as the primary address, and life was good....for about 10 minutes, until administrators started whining about wanting to create a mailbox, change the default address, and NOT have the old address available.
It turns out, that due to latency, you can't simply do:
set-mailbox -identity $user -PrimarySmtpAddress $newaddressstring -EmailAddressPolicyEnabled $false
$mailbox = Get-Mailbox -identity $user
$mailbox.EmailAddresses | foreach { if (!$_.IsPrimaryAddress -and ($_.PrefixString -eq 'SMTP')) {$mailbox.EmailAddresses -= $_}}
set-mailbox -identity $user -EmailAddresses $mailbox.EmailAddresses
So, I came up with this:
$newaddressstring = "karlmitschketest@testmail.com"
$user = "ktester"
set-mailbox -identity $user -PrimarySmtpAddress $newaddressstring -EmailAddressPolicyEnabled $false
do{$address = get-mailbox -identity $user |select PrimarySmtpAddress}while ($address.PrimarySmtpAddress.ToString().ToLower() -ne $newaddressstring)
$mailbox = Get-Mailbox -identity $user
$mailbox.EmailAddresses | foreach { if (!$_.IsPrimaryAddress -and ($_.PrefixString -eq 'SMTP')) {$mailbox.EmailAddresses -= $_}}
set-mailbox -identity $user -EmailAddresses $mailbox.EmailAddresses
This works in the EMS, and I use she same basic function in my C# aspx page.
Karl