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(@"SVC_CubitDLE@lyse.no", @".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 SVC_CubitDLE@lyse.no ".whb----%RS*,H^"
S: E00000002 NO LOGIN failed.
Any idea to what I'm doing wrong here?