Authenticating to GoDaddy email via MailKit / IMAP and OAuth2.0

48 Views Asked by At

I am maintaining a headless (runs as a background service) application that reads emails automatically (it does not send, just parses, handles, and deletes). This had been working for years prior to MS discontinuing legacy application support in connecting to email and now it needs to be updated to connect via GoDaddy/Exchange at Azure.

I have tried to follow the guides on connecting MailKit with OAuth 2.0 (here: https://github.com/jstedfast/MailKit/blob/master/ExchangeOAuth2.md), but I keep getting "Authentication Failed" errors.

Here is the code in question...

var confidentialClientApplication = ConfidentialClientApplicationBuilder.Create($"{clientId")
    .WithAuthority($"https://login.microsoftonline.com/{tenantId}/v2.0")
    .WithClientSecret("<secret>")
    .Build();

var scopes = new string[]
    {
        "https://ps.outlook.com/.default"
    };

var authToken = await confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync();
var oauth2 = new SaslMechanismOAuth2(User, authToken.AccessToken);

using (ImapClient imapClient = new ImapClient())
{
    await imapClient.ConnectAsync(Server, Port, SSL);
    if (imapClient.IsConnected)
    {
        await imapClient.AuthenticateAsync(oauth2);

    }
}

The call to ConnectAsync is successful, but when I call the AuthenticateAsync method, I get an error "Authentication Failed".

I have followed the guides and created a Application in my Azure Tenant. I provisioned it with the appropriate scopes and IMAP guides. However, as this is a headless application (no user interaction) I don't know how to have the Application Endpoint setup? Perhaps this is my problem?

0

There are 0 best solutions below