0 votes
by (180 points)

In the past we used imap impersonation this way.

Imap imap = new Imap();
// credentials for authentication
string loginUserName = ...;
string password = ...;
// user name of impersonated user
string mailboxUserName = ...;
// construct a username for impersonation
string userName = mailboxUserName + "\0" + loginUserName;
// authenticate to the server
imap.Login(userName, password, ImapAuthentication.Plain);

Now we are using Office365 and OAuth authentication.

Is it still possible to use imap impersonation with Office365 (OAuth). If yes, how?

1 Answer

0 votes
by (15.2k points)


yes, it is possible using code like this:

// receive your OAuth token from office 365 server
string oAuthToken = ReceiveOAuthToken();

// correct permission has to be set on Azure/Office 365
// same approach can be used for accessing shared mailbox
// using the shared mailbox address,
// e.g "shared.mailbox@yourcompany.com"
string mailboxUserName = "impersonated-user@yourcompany.com"; 


string pattern2 = string.Format(
      "user={0}{1}auth=Bearer {2}{1}{1}",
      mailboxUserName, '\x1', oAuthToken);

string token2 = Convert.ToBase64String(

client.Login(token2, Rebex.Net.ImapAuthentication.OAuth20);

For more info how to use OAuth on Office 365 please read our blog post how to login with OAuth 2.0 to Office365 with Rebex Secure Mail.