Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to read an email from my mailbox using the MailKit library. Unfortunately the program throws this MailKit.Security.AuthenticationException: 'LOGIN failed.'
The credentials match (I tried to log in through the browser)
I'm trying to read an email from my mailbox using the MailKit library. Unfortunately the program throws this

MailKit.Security.AuthenticationException: 'LOGIN failed.'

I would be grateful for any help, I don't know what to do. I googled, tried this
C#
client.Connect("imap.outlook.com", 993, SecureSocketOptions.None);
But nothing came up when I changed
C#
SecureSocketOptions
to
C#
Auto
the same exception was thrown

My code:
using (var client = new ImapClient())
            {
                using (var cancel = new CancellationTokenSource())
                {
                    client.Connect("imap.outlook.com", 993, true);

                    
                    //client.AuthenticationMechanisms.Remove("XOAUTH");

                    client.Authenticate("mail@test.cz", "password", cancel.Token);

                    
                    var inbox = client.Inbox;
                    inbox.Open(FolderAccess.ReadOnly, cancel.Token);

                    Console.WriteLine("Total messages: {0}", inbox.Count);
                    Console.WriteLine("Recent messages: {0}", inbox.Recent);

                   
                    for (int i = 0; i < inbox.Count; i++)
                    {
                        var message = inbox.GetMessage(i, cancel.Token);
                        Console.WriteLine("Subject: {0}", message.Subject);
                    }

                   
                    var query = SearchQuery.DeliveredAfter(DateTime.Parse("2013-01-12"))
                        .And(SearchQuery.SubjectContains("MailKit"))
                        .And(SearchQuery.Seen);

                    foreach (var uid in inbox.Search(query, cancel.Token))
                    {
                        var message = inbox.GetMessage(uid, cancel.Token);
                        Console.WriteLine("[match] {0}: {1}", uid, message.Subject);
                    }

                    client.Disconnect(true, cancel.Token);
                }
            }


What I have tried:

Checked the credentials in Browser, switched Options in Client
Posted
Comments
Richard Deeming 25-Jul-22 6:17am    
Try using the MailKit protocol logger to see the raw IMAP communication in case that gives you any clues:
ProtocolLogger Class[^]
Petr Barabáš 25-Jul-22 6:51am    
It says the same.
Richard Deeming 25-Jul-22 6:53am    
Seriously? Read the protocol log, not the error message!
Petr Barabáš 25-Jul-22 7:00am    
I've used the example from your link, where I got errors. That Logger I cannot convert to string...
Richard Deeming 25-Jul-22 7:42am    
The example creates a file called smtp.log in the application directory, which contains the log of the IMAP conversation.

 
Share this answer
 
You have to enable the 2 factor authentication on your email account, then create an app password and use
this password in your program.

Check this link "Using app passwords with apps that don't support two-step verification".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900