2
votes

For a lot of customers, we connect to the Office 365 Outlook IMAP server, and that works just fine. However, for one customer now it does not work. I can log into portal.office.com with the username and password, and go to Outlook there and see the mailbox, but I can't for the life of me get this to work in my code. This is my code:

var stream = new MemoryStream();
var logger = new ProtocolLogger(stream, false);

using (var client = new MailKit.Net.Imap.ImapClient(logger))
{
    try
    {
        client.Connect("outlook.office365.com", 993, true);
        client.AuthenticationMechanisms.Remove("XOAUTH2");
        client.Authenticate(@"[email protected]", @".whb----%RS*,H^");
        var inbox = client.Inbox;
        inbox.Open(FolderAccess.ReadWrite);
        var uids = await inbox.SearchAsync(SearchQuery.NotSeen);
    } catch (Exception exc){
        exc.Dump();
        stream.Position = 0;
        var reader = new StreamReader(stream);
        reader.ReadToEnd().Dump();
    }
}

I tried with and without the line client.AuthenticationMechanisms.Remove("XOAUTH2");.

The exception thrown is AuthenticationException with message LOGIN failed.

The log emitted in the steam is:

S: * OK The Microsoft Exchange IMAP4 service is ready. [UABSADAAUAAyADYANABDAEEAMAAxADkAOAAuAEYAUgBBAFAAMgA2ADQALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]
C: E00000000 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
S: E00000000 OK CAPABILITY completed.
C: E00000001 AUTHENTICATE PLAIN AFNWQ19DdWJpAAAAAAAAAAAAAAAAAAAASF4=
S: E00000001 NO AUTHENTICATE failed.
C: E00000002 LOGIN [email protected] ".whb----%RS*,H^"
S: E00000002 NO LOGIN failed.

Any idea to what I'm doing wrong here?

1
It looks as if you've pasted an actual password, twice. Might want to change that straight away. - arnt
(FYI, AUTHENTICATE PLAIN contains the password base64-encoded, too.) - arnt
I've looked at the actual question now, and I think this is a matter for Microsoft's customer service. The IMAP parts look faultless. - arnt
I missed the base64 part, but the other passwords above is not the correct ones. Thanks for your comments. - Øyvind Bråthen
I'm a bit late to the party and I'm assuming that the username and password strings have been obfuscated now(?), so I can only ask: does the user name or password contain any unicode characters? The only thing that I can think of that might break AUTHENTICATE PLAIN is if MailKit's SaslPrep implementation is wrong somehow, but that would require the username or password to contain unicode characters which does not seem to be the case. - jstedfast

1 Answers

0
votes

It is very possible that IMAP is disabled for that user. I would check to see if that's enable if you have access to the AD.

Here's an article