Posts Tagged ‘shared’

Access shared/delegate mailbox of Office 365

Thursday, February 4th, 2021

There are 2 ways of accessing a shared mailbox in Office 365: first -using regular IMAP authentication and a second one – using OAuth 2.0.

Shared mailbox – basic authentication

Make sure basic authentication is turned on:

Try authenticating to users’ regular mailbox with his credentials to check if basic authentication works and the user/password is correct:

client.UseBestLogin("AlexW@example.com", "AlexWPassword");

Access the shared mailbox

Use the following user format:

Username@DomainName\Shared@DomainName

You must use Login method:

client.Login(@"AlexW@example.com\invoices@example.com", "AlexWPassword"); 

-or-

alternatively you may use LoginPlain method:

client.LoginPlain("invoices@example.com", "AlexW@example.com", "AlexWPassword");

Don’t use UseBestLogin for Office365 shared mailboxes and basic authentication.

The reason is Office  365 advertises LOGIN PLAIN as a preferred login method and UseBestLogin chooses to use it. However Office 365 does not recognize Username@DomainName\Shared@DomainName user pattern when using LOGIN PLAIN. For Office 365 to accept a LOGIN PLAIN to a shared mailbox, you need to provide all 3 parameters separately: shared mailbox, user and user password – use LoginPlain to do that.

Shared mailbox – OAuth 2.0

It is much simpler with OAuth. Use any of the available OAuth 2.0 flows:

https://www.limilabs.com/blog/oauth2-password-grant-office365-exchange-imap-pop3-smtp

https://www.limilabs.com/blog/oauth2-office365-exchange-imap-pop3-smtp

When you’ve obtained an access token you simply use it to access shared mailbox:

string accessToken = result.AccessToken;

using (Imap client = new Imap())
{
    client.ConnectSSL("outlook.office365.com");
    client.LoginOAUTH2("invoices@example.com", accessToken);

    client.SelectInbox();

    List<long> uids = imap.Search(Flag.Unseen);
    foreach (long uid in uids)
    {
        var eml = imap.GetMessageByUID(uid)
        IMail email = new MailBuilder().CreateFromEml();
        string subject = email.Subject);
    }
    client.Close();
}

Access shared/delegate mailbox of Exchange Server

Friday, April 4th, 2014

1. Enable IMAP and POP3 protocols

Make sure you have enabled IMAP and POP3. Following articles can help you with that:

2. Turn on basic authentication

Using Exchange Management Console

  • Open EMC, expand to Server Configuration->Client Access.
  • In the middle panel, click your exchange CAS server, click POP3 and IMAP4 tab, right click IMAP4 and choose properties.
  • In Authentication tab, select “Plain text logon (Basic authentication)”, then click OK.
  • Open services.msc, restart Microsoft Exchange Transport services.

Using Power Shell

Set-IMAPSettings -Server -LoginType PlainTextLogin
Set-POPSettings -Server -LoginType PlainTextLogin

Open services.msc, restart Microsoft Exchange Transport services.

3. Add permissions to the shared mailbox

Give one user full access permission to the shared mailbox:

Add-MailboxPermission Shared.Mailbox1 -user John.Doe -AccessRights FullAccess

This also can be configured from EMC (Exchange Management Console) gui, by selecting the mailbox and clicking on “Manage Full Access Permission…” at the right pane of the window.

Note: You can not add permissions for user without mailbox, Powershell is the only option in such case.

4. Access the shared mailbox

Exchange 2003

Use the following user format DomainName\Username\SharedMailboxAlias (note the use of SharedMailboxAlias it’s often same as the name but it may be different)
(e.g. DOMAIN\John.Doe\Shared.Mailbox1) to log into the shared mailbox.

Exchange 2007

Install this patch: http://support.microsoft.com/?kbid=949926

Use the following user format DomainName\Username\SharedMailboxAlias (note the use of SharedMailboxAlias it’s often same as the name but it may be different)
(e.g. DOMAIN\John.Doe\Shared.Mailbox1) to log into the shared mailbox.

Exchange 2010, Exchange 2013, Exchange 2016, Exchange 2019

Use the following user format DomainName\Username\SharedMailboxAlias (note the use of SharedMailboxAlias it’s often same as the name but it may be different)
(e.g. DOMAIN\John.Doe\Shared.Mailbox1) to log into the shared mailbox.

Office 365/Exchange Online

You can find more details here: https://www.limilabs.com/blog/shared-mailbox-office365

Use the following user format Username@DomainName\Shared@DomainName

You must use Login method:

client.Login(@"Username@DomainName\Shared@DomainName", "UserPassword"); 

-or- LoginPlain method:

client.LoginPlain("Shared@DomainName", "User@DomainName", UserPassword");

Don’t use UseBestLogin for Office365 shared mailboxes.

Using LoginPLAIN to login as a different user

You can try 3rd parameter of LoginPLAIN to log in as a different user:

using (Imap imap = new Imap) 
{ 
    imap.ConnectSSL("outlook.office365.com"); 
    imap.LoginPLAIN("SharedMailboxAlias", "Username@DomainName", "UserPassword"); 
    //... 
    imap.Close(); 
}