MS Exchange, Office 365, Shared Mailboxes

MailBee can work with MS Exchange (and Office 365 which is powered by it) via both common IMAP/SMTP protocols and proprietary Exchange Web Services (EWS) protocol. MailBee components to use are Imap/Smtp and Ews, respectively.

SMTP/IMAP pros, EWS cons

If your code is supposed to work with different mail servers, not just Exchange, using SMTP/IMAP lets you write code just once. EWS can work with Exchange only.

EWS pros, SMTP/IMAP cons

SMTP and especially IMAP services can often be turned off in Exchange. They also operate on ports which are often blocked by firewalls. EWS, on other hand, operates on standard HTTP/HTTPS ports.

SMTP/IMAP support in Exchange is limited. For instance, search of international strings in Exchange's IMAP is not possible, only plain ASCII strings (while IMAP standard in theory allows for using UTF-8 for search queries). Featureset of EWS is much wider. You can even add/remove attachments in mail messages while in IMAP mail messages are immutable.

SMTP/IMAP can be enabled on the Exchange server but disabled for particular user. EWS is usually enabled for most users. See MS Exchange troubleshooting for more details.

EWS Managed API

To use Ews component, you need to install EWS Managed API from Microsoft. You can add it as a reference from C:\Program Files (x86)\MailBee.NET Objects\Microsoft.Exchange.WebServices.dll (Microsoft.Exchange.WebServices.NETStandard.dll in case of .NET Core). Or you can do it with NuGet:

Install-Package MailBee.NET.EWS

EWS is supported in .NET Framework 3.5/4.0/4.5+ and .NET Core 2.0+.

Due to limitations of EWS Managed API libraries, you have to use synchronous methods in case of .NET Framework and asynchronous methods in case of .NET Core.

The detailed example of working with EWS can be found in Ews topic.

If you're on .NET 4.5 and experiencing TLS 1.0 related error when making EWS queries, upgrade your solution to a newer .NET version. Alternatively, add this at the top of your code:

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12

Office 365

Office 365 supports both IMAP/SMTP and EWS so it's up to you to decide which option to select (based on pros and cons above). The access details:

Office 365 no longer supports basic authentication (login/password) for IMAP/POP3/SMTP. You'll need to use OAuth 2.0 for that. See OAuth 2.0 for Office 365 Accounts (non-interactive mode) tutorial and other guides and samples at OAuth 2.0 in Windows, .NET Core, and ASP.NET MVC apps section on how to enable OAuth 2.0 IMAP/POP3/SMTP with Office 365.

With EWS, see Ews.SetCredentials(ExchangeCredentials) overload for more information on how to use OAuth 2.0 for authentication. Impersonated access topic can be helpful too.

Shared mailbox access

To access shared mailbox's folders in MS Exchange or Office 365 via IMAP, you should log in with YourAccount@domain.com\SharedMailboxAlias username and your account's password. You'll be logged in a shared mailbox instead of your normal account. From that, you can proceed as usually.

With EWS, it's different. You log in normally you and then select a folder in the shared mailbox you want to work with:

// It's assumed ewsClient is Ews instance, with credentials and server host already set.
FolderId id = new FolderId(WellKnownFolderName.Inbox, "shared-primary-smtp-address@domain.com");

// Demonstrate uploading a message in the folder.
MailBee.Mime.MailMessage msg = new MailBee.Mime.MailMessage();
ewsClient.UploadMessage(id, msg, false);

// Demonstrate getting the folder.
EwsFolder folder = ewsClient.DownloadFolderById(id); 

// Demonstrate getting the folder contents. 
EwsItemList sentItems = ewsClient.DownloadItems(id, null, false, EwsItemParts.GenericItem);
' It's assumed ewsClient is Ews instance, with credentials and server host already set.
Dim id As New FolderId(WellKnownFolderName.Inbox, "shared-primary-smtp-address@domain.com")

' Demonstrate uploading a message in the folder.
Dim msg As New MailBee.Mime.MailMessage()
ewsClient.UploadMessage(id, msg, False)

' Demonstrate getting the folder.
Dim folder As EwsFolder = ewsClient.DownloadFolderById(id)

' Demonstrate getting the folder contents. 
Dim sentItems As EwsItemList = ewsClient.DownloadItems(id, Nothing, False, EwsItemParts.GenericItem)

Important: in IMAP, you specify an alias of a shared mailbox, in EWS - its primary SMTP address. They are often the same but can be different sometimes.

Impersonated access

To use impersonated access, you'll need to add this to your code (in additon to SetCredentials):

ewsClient.Service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, account);
ewsClient.Service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, account)

The required namespace is Microsoft.Exchange.WebServices.Data. account is the SMTP e-mail address of the account in question.

In case if you're using OAuth 2.0 for authentication, You may also need to add the EWS.AccessAsUser.All permission and then define the application access policy. See Limiting application permissions to specific Exchange Online mailboxes how.


Send feedback to AfterLogic

Copyright © 2006-2024 AfterLogic Corporation. All rights reserved.