0 votes
by (200 points)

OAuth20 authentication is failing for Gmail.

Below is the smtpException I am getting:
Rebex.Net.SmtpException: OAuth failed: OAuth authentication failed due to Invalid token. Code -2147184118

For gmail, I'd given full scope - mail.google.com/ but nothing is mentioned for outlook. This is what I've given as scope for outlook - openid+offline_access+profile+https:%2f%2foutlook.office.com%2fmail.send

Below is the code snippet:

try{
        Smtp client = new Smtp();
        client.Connect("smtp-mail.outlook.com", SslMode.Explicit);

        // prepare token
        string pattern = string.Format("user={0}{1}auth=Bearer {2}{1}{1}", userEmailId, '\x1', accessToken);
        string token = Convert.ToBase64String(Encoding.ASCII.GetBytes(pattern));

        client.Login(token, SmtpAuthentication.OAuth20);
    }
    catch(SmtpException ex)
    {}
}

Kindly let me know if there is anything that I'm doing wrong here. I could make this work with Gmail. I've also tried to use IMAP instead of SMTP for outlook but no result.

Regards
Mrinal

1 Answer

0 votes
by (62.7k points)
edited by
 
Best answer

We wrote a blopost about Gmail OAuth 2.0 authentication.

It contains sample application you can try.

Update: We published a blog post that describes how to login with OAuth 2.0 to Office365 with Rebex Secure Mail, and another one that describes how to register application for with appropriate permissions in Azure.


For Outlook 365 I found this answer on StackOverflow:

We don't support OAuth for IMAP access to Office 365 ... OAuth is supported for Office 365 REST APIs ... and our older SOAP APIs Exchange Web Services.

It seems the OAuth for SMTP is not supported as well. However OAuth for EWS is supported. You can use Rebex.Net.Ews class in this case.

And the last thing. In OAuth for EWS we are successfully using the access token directly (without any processing). I suggest you to try it as well in case of reported invalid token error. So, your code can look like this:

//client.Login(token, SmtpAuthentication.OAuth20);
client.Login(accessToken, SmtpAuthentication.OAuth20);
by (200 points)
Tried this for EWS Client. It seems there's no Login method for (accessToken, Authentication). Can you please verify?
by (58.9k points)
edited by
Hi, you are right, the OAuth support for our EWS client is going to be released as part of the next version of Rebex components.
by (58.9k points)
Hi, the OAuth support in EWS client has been released as part of Rebex Secure Mail 2016 R3 version.
...