nadun
nadun

Reputation: 29

Office365 Imap not working programatically

Trying to connect to imap using MailKit library. While this code works for Gmail and hot mail. It gives Login Failed error for Office 365.

using (var client = new ImapClient(new ProtocolLogger("imap.log")))
{
      client.Timeout = 120000;
      client.Connect("outlook.office365.com", 
                      93,SecureSocketOptions.SslOnConnect);
      client.Authenticate("[email protected]", "XXXXXPassword");
      client.Inbox.Open(FolderAccess.ReadOnly);

      var uids = client.Inbox.Search(SearchQuery.All);

      foreach (var uid in uids)
      {
         var message = client.Inbox.GetMessage(uid);
         // write the message to a file
         message.WriteTo(string.Format("{0}.eml", uid));
     }
     client.Disconnect(true);
}

Upvotes: 2

Views: 1381

Answers (1)

Kalu
Kalu

Reputation: 66

Microsoft has Deprecated basic authentication starting from 2023. You can read more about it on their article Deprecation of Basic authentication in Exchange Online

You need to use Oauth2 authentication Authenticate an IMAP, POP or SMTP connection using OAuth

Upvotes: 1

Related Questions