MS Exchange, Office 365, Shared Mailboxes

MailBee can work with Exchange (and Office 365 which is powered by Exchange 2016) 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.

SMTP/IMAP can be used with .NET Core and UWP apps. EWS support is currently available for classic .NET Framework v3.5-4.7 only (it relies on EWS Managed API Microsoft hasn't ported to .NET Core yet). This will hopefully change soon.

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.

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:

Shared mailbox access

To access shared mailbox's folders in MS Exchange or Office 365 via IMAP, you should log in with\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, "");

// 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, "")

' 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.

Send feedback to AfterLogic

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