Project Description

How to manage Outlook 2003 and 2007 email accounts with .Net and the Powershell?

This projects provides a class library, some cmdlets and some scripting samples to do it!

This is currently the only way to update an Outlook account from .Net, wrapping the official Microsoft Account Management API

Follow alexpilotti on Twitter

Class Library

The provided OutlookAccountManager.dll assembly wraps the Account Management API (IOlkAccountManager, etc.), which are quite poorly documented here:
http://msdn.microsoft.com/en-us/library/bb821156.aspx

Being a set of COM interfaces, not available through COM interop, the easiest way to wrap the API and provide a higher level access was to develop a simple mixed mode C++/CLI assembly.

The main MAPI features are provided via some static methods in the AccountManager class.

Usage is really straightforward, here are some samples:

using AlexPilotti.OutlookAccountManager;

...

// Initialize MAPI
AccountManager.MAPIInitialize();

// Get the available MAPI profiles list
IList<ProfileData> profiles = AccountManager.GetProfiles();

// Get the MAPI accounts in the first profile (e.g. "Outlook")
IList<AccountData> accounts = AccountManager.GetAccounts(profiles[0].ProfileName);

// Do some changes in the account settings
accounts[0].EmailID = "changed.email@acme.com";
accounts[0].OutgoingServer = "new.smtpserver.com";

// Update the account
AccountManager.SaveAccountData(accounts[0]);

// That's it!
AccountManager.MAPIUninitialize();


Requirements:

Microsoft Visual C++ 2008 Redistributable Package (x86)
http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&displaylang=en

Powershell Cmdlets

Three sample cmdlets are provided in the OutlookAccountManagerPS Powershell SnapIn:

  • Get-MAPIProfile
  • Get-MAPIAccount
  • Set-MAPIAccount


Installing the snap in is easy. In the Powershell, run the following commands as administrator:

set-alias installutil $env:windir\Microsoft.NET\Framework\v2.0.50727\installutil
cd <The dir where you unzipped/copied the assembly>
install OutlookAccountManagerPS.dll



Ok, now the cmdlets are available to any user:

# Load the snap in
add-pssnapin OutlookAccountManagerSnapIn

# Get all the accounts in the Outlook profile
Get-MAPIAccount "Outlook"



A slightly more complex example is available in the OutlookAccountBackup.ps1 script.

The aim of this script is to provide a simple and quick way to change the SMTP server address of all your non Exchange Outlook accounts when your network settings require a mandatory SMTP server.
This tipically happens when using a laptop as a guest on some LAN, or using mobile connections, with anti spam measures enforced.

Don't forget to load the script with the standard "dot source" syntax:

. <path to the script>\OutlookAccountBackup.ps1

# Backup account data
BackupOutlookAccounts

# Change the SMTP server to the given one ...
ChangeOutlookAccountsSMTPServer "smtp.tre.biz"

# ... and finally restore the previous settings:
RestoreOutlookAccountsSMTPServer



Here's the full script sources, also available in the downloads section:

# Copyright (c) Alessandro Pilotti 2009

add-pssnapin OutlookAccountManagerSnapIn -ErrorAction SilentlyContinue

# Backup all the Outlook profile accounts to "$home\OutlookAccounts.xml"
#
function BackupOutlookAccounts()
{
	$a = Get-MAPIAccount "Outlook"
	$a | Export-Clixml $home\OutlookAccounts.xml
}

# Changes the SMTP server in all the non Exchange Outlook accounts.
# This is useful in cases of usage of temporary networks requiring a mandatory SMTP server.  
# Call "BackupOutlookAccounts" before this function, in order to backup the account data
# for a later restore with "RestoreOutlookAccountsSMTPServer"
#
function ChangeOutlookAccountsSMTPServer($smtpServerName)
{
	if(!$smtpServerName) {
		throw("smtpServerName parameter value is missing")
	}

	$a = Get-MAPIAccount "Outlook" | where-object {!$_.IsExchange}
	$a | % {
		$_.OutgoingServer = $smtpServerName; 
		$_.OutgoingPort = 25; 
		$_.OutgoingUseAuthentication = $false; 
		$_.OutgoingSSL = $false; 
		$_.OutgoingSPA = $false;
		Set-MAPIAccount $_;
	}
}

# Restores the SMTP server to the original value on all non Exchange accounts.
# Useful to switch back to the standard SMTP settings after 
# a call to "ChangeOutlookAccountsSMTPServer".	
#
function RestoreOutlookAccountsSMTPServer()
{
	$a = Get-MAPIAccount "Outlook" | where-object {!$_.IsExchange}
	$b = Import-Clixml $home\OutlookAccounts.xml

	foreach($i in $a) {
		$b | % {
			if($_.AccountId -eq $i.AccountId) {
			
				$i.OutgoingServer = $_.OutgoingServer; 
				$i.OutgoingUseAuthentication = $_.OutgoingUseAuthentication;
				$i.OutgoingPort = $_.OutgoingPort; 
				$i.OutgoingSSL = $_.OutgoingSSL; 
				$i.OutgoingSPA = $_.OutgoingSPA; 
				Set-MAPIAccount $i;  
			}			
		}
	}	
}

 

Sample App

A simple app is available to test the class library features, including loading and saving accounts.

SampleApp.png

Notes

The project contains some work from http://www.codeproject.com/KB/IP/IOlkAccountManager.aspx by Ashutosh Bhawasinka

OutlookSpy has been very useful due to the lack of official documentation
http://www.dimastr.com/outspy/


Alessandro Pilotti

Last edited Oct 31, 2012 at 9:46 AM by alexp, version 18