'How to connect to a specific Outlook/Exchange account?

I am making an application to access a particular email account hosted on an Exchange 2003 server and do some stuff to the unread emails found. I do not really know very much about how MAPI works, so I wouldn't be too surprised if I have missed something very obvious! I am (unfortunately) using C#, I have read about the issues this may cause (and am dreading the repercussions, but my boss wants it done like this so..).

I am struggling to find any good info on how to connect to a specific account! The application will be run from someones laptop (let's say mine), so there will be a default account that Outlook connects to when opened. So:

  • Is it possible to connect to another account from a machine where the user already has there own account and probably has Outlook open?

  • If it is possible. How do I do this? When initiating the Outlook interop objects the application automatically gets the users account and sets the current user to that. I had hoped the Logon() method would sort this but no. Even just running Outlook.Application olApp = new Outlook.Application(); Goes and sets the current user to the standard account.

I hope I am making sense (probably not), but feel free to ask more detailed questions in the comments and I will reply as quickly as possible. Like I said, I know very little about MAPI and Exchange so I am struggling with how to phrase my question.



Solution 1:[1]

If you have 2 accounts defined on one machine, you can use following: example

Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");

// optional
//object missing = Type.Missing;
//ns.Logon(missing, missing, true, false);

// additional email address 
string recipientName = "myEmail@myDomain";

Outlook.Recipient recip = ns.CreateRecipient(recipientName);
recip.Resolve();

if (recip.Resolved)
{
Outlook.MAPIFolder inboxFolder = ns.GetSharedDefaultFolder(recip, Outlook.OlDefaultFolders.olFolderInbox);
}

Solution 2:[2]

In OOM, you can use Namespace.GetSharedDefaultFolder (assuming the current user has access right to that shared folder).

You can also use Redemption (I am its author) for that - run your code as a domain user who can access the mailboxes in question, call RDOSession.LogonExchangeMailbox for that user, then open other users' mailboxes using RDOSession.GetSharedMailbox/GetSharedDefaultFolder.

Since Exchange 2013 no longer allows old style RPC connections (only RPC-over-HTTP or MAPI-over-HTTP), you can use RDOSession.LogonHostedExchangeMailbox (works for both Exchange 2013 and Exchange 2010).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2