0
votes

I want to read an Outlook mailbox using PowerShell. I can access the Outlook mailbox using a MAPI API call, but I can only connect with an existing/preconfigured Outlook ID/profile on the machine. My requirement is, I need to connect to Outlook mailbox during runtime.

I am using PowerShell version 5.

importing assembly files

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application

mapping Namespace

$namespace = $Outlook.GetNameSpace("MAPI")

accessing inbox

$OutlookInbox = $Outlook.Session.GetDefaultFolder(6)
$OutlookFolders = ($Outlook.Session.Folders.Item(1).Folders.Item(2)).FullFolderPath
2

2 Answers

1
votes

Firstly, this is not MAPI: you just pass string "MAPI" to the Application.GetNamespace call - this is Outlook Object Model. It only allows you to access a mailbox in a preconfigured local profile. If there are multiple local profiles and Outlook is not running, you can pass the name of the profile (as shown in Control Panel | Mail | Show Profiles) to Namespace.Logon. If Outlook is already running, Namespace.Logon won't do anything. If the primary mailbox in the profile has the right to access other mailboxes in the same Exchange org, you can use Namespace.CreateRecipient / Namespace.GetSharedDefaultFolder to access the default folders of other mailboxes.

If you want truly dynamic access to an arbitrary mailbox without an existing Outlook profile, you can either:

  1. Use EWS library - it is accessible from PS: see https://blogs.msdn.microsoft.com/webdav_101/2018/06/19/about-using-ews-and-powershell/
  2. A temporary profile can be created and configured using Extended MAPI (see https://blogs.msdn.microsoft.com/dvespa/2015/10/28/how-to-configure-an-outlook-2016-profile-using-mfcmapi/), but Extended MAPI can only be accessed from C++ or Delphi.
  3. You can use Redemption - it exposes RDOSession.LogonHostedExchangeMailbox method (RDOSession object roughly corresponds to the Namespace object in the Outlook Object Model). LogonHostedExchangeMailbox creates (and then deletes) a temporary profile that points to the specified mailbox.
0
votes

Currently, you are dealing with the Outlook object model, not MAPI.

Outlook is a singleton, you can't run two instances on the system at the same time. So, when you create a new Application instance you will be linked to the already running instance. Be aware, you need to run both applications under the same security context.

Note, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

As a workaround, you may consider using Open XML SDK if you deal only with Exchange accounts, see Start using web services in Exchange for more information.