Search code examples
c#emailauthenticationimapmailkit

C# Mailkit - IMap Unable Open/Examine Inbox due to Unsafe Login?


The following code is how I connect to my IMap server, this seems to connect to other IMap servers without an issue & get authenticated but it is not allowing me to open the inbox because of unsafe login?

The line inbox.Open(FolderAccess.ReadOnly) is where the code is breaking and throwing the following error.

The IMAP server replied to the 'EXAMINE' command with a 'NO' response: EXAMINE Unsafe Login. Please contact kefu@188.com for help

I have tried to enable SSL but still the same result. I am able to login to this EMail using other email clients such as Microsoft Mail.

    public static int getResult(string ToEmail, string imapServer, int port, string user, string pwd, DateTime timeCodeRequested)
    {
        using (var client = new ImapClient(new ProtocolLogger("C:/log/log.txt")))
        {
            client.ServerCertificateValidationCallback = (s, c, h, e) => true;

            // Remove the XOAUTH2 authentication mechanism since we don't have an OAuth2 token.
            client.AuthenticationMechanisms.Remove("XOAUTH2");

            client.Connect(imapServer, port, true);

            client.Authenticate(user, pwd);

            TryAgain:
            // The Inbox folder is always available on all IMAP servers...
            var inbox = client.Inbox;
            inbox.Open(FolderAccess.ReadOnly);

            IList<OrderBy> orderBy = new[] { OrderBy.ReverseArrival };
            IList<UniqueId> ids = inbox.Sort(SearchQuery.FromContains("sh@n.com")
                .And(SearchQuery.ToContains(ToEmail))
                .And(SearchQuery.DeliveredAfter(timeCodeRequested)
                .And(SearchQuery.YoungerThan(80))), orderBy);

            if (ids.Count == 0)
            {
                goto TryAgain;
            }
            else
            {
                foreach (UniqueId id in ids)
                {
                    var message = inbox.GetMessage(id);
                    var ex = message.Subject.ToString();
                    ex = ex.Substring(ex.Count() - 6);

                    try
                    {
                        return Convert.ToInt32(ex);
                    }
                    catch
                    {
                        continue;
                    }
                }
                goto TryAgain;
            }
        }
    }

Here is the output of the IMap log C:/log/log.txt, I have removed the IMap credentials

S: * OK Coremail System IMap Server C: A00000000 CAPABILITY S: * CAPABILITY IMAP4rev1 XLIST SPECIAL-USE ID LITERAL+ STARTTLS XAPPLEPUSHSERVICE UIDPLUS X-CM-EXT-1 S: A00000000 OK CAPABILITY completed C: A00000001 LOGIN email password S: A00000001 OK LOGIN completed C: A00000002 CAPABILITY S: * CAPABILITY IMAP4rev1 XLIST SPECIAL-USE ID LITERAL+ STARTTLS XAPPLEPUSHSERVICE UIDPLUS X-CM-EXT-1 S: A00000002 OK CAPABILITY completed C: A00000003 LIST "" "" S: * LIST (\Noselect) "/" "" S: A00000003 OK LIST Completed C: A00000004 LIST "" "INBOX" S: * LIST () "/" "INBOX" S: A00000004 OK LIST Completed C: A00000005 LIST (SPECIAL-USE) "" "*" S: * LIST (\Drafts) "/" "&g0l6P3ux-" S: * LIST (\Sent) "/" "&XfJT0ZAB-" S: * LIST (\Trash) "/" "&XfJSIJZk-" S: * LIST (\Junk) "/" "&V4NXPpCuTvY-" S: A00000005 OK LIST Completed C: A00000006 EXAMINE INBOX S: A00000006 NO EXAMINE Unsafe Login. Please contact kefu@188.com for help


Solution

  • It seems that your IMAP server admin has decided to allow the LOGIN command but considers it too insecure to allow you to actually open folders when logged in using that method of authenticating.

    Normally, MailKit attempts to use SASL authentication if it is available, but your IMAP server does not seem to advertise support for any SASL authentication mechanisms.

    In these cases, I would usually recommend trying to connect to the SSL port or at least try using STARTTLS (which your server does support):

    client.Connect (host, port, SecureSocketOptions.StartTls);
    

    However, you said that this error persists even after using SSL which means I have no other suggestions to give you other than to follow the suggestion in the error message and email kefu@188.com for help.