Mailkit Authenticate to Imap fails

2019-05-31 21:47发布

问题:

I'm trying to use MimeKit and MailKit to fetch e-mail from inbox.
But the logon to the mail server Imap does not work.
I'm using port 993 with ssl.
I have tried to connect/LOGIN/FETCH on port 143 with Telnet and it works.
I have tried to connect/LOGIN/FETCH on port 993 with Openssl and it works.
Mailkit v.1.4.2.1

KODE:

     using (var client = new ImapClient(new ProtocolLogger("imap.log")))
        {
            try
            {
                client.Connect(server, this.port, true);
                client.AuthenticationMechanisms.Remove("XOAUTH2");                    
                client.Authenticate(user, password);
                var inbox = client.Inbox;
                inbox.Open(FolderAccess.ReadWrite);

                if (inbox.Count > 0)
                {
                    var range = Enumerable.Range(0, inbox.Count).ToArray();
                    inbox.AddFlags(range, MessageFlags.Deleted, false);
                    inbox.Expunge();
                }
                client.Disconnect(true);
            }
            catch (AuthenticationException e)
            {
                throw e;
            }
        }

IMAP.LOG:

    Connected to imaps://xxxx.xxx.xx:993/
    S: * OK The Microsoft Exchange IMAP4 service is ready.        
    C: A00000000 CAPABILITY
    S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=NTLM AUTH=GSSAPI UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+
    S: A00000000 OK CAPABILITY completed.
    C: A00000001 AUTHENTICATE NTLM
    S: +
    C: TlRMTVNTUAABAAAABwIIAAAAAAAgAAAAAAAAACAAAAA=
    S: +
    TlRMTVNTUAACAAAABgAGADgAAAAFAokCETTDB74D2YsAAAAAAAAAAKIAogA+AAAABgLwIwAAAA9LAE0ARAACAAYASwBNAEQAAQAUAEsATQBEAEUAWABDADIAMAAwADYABAAaAGkAbgB0AGUAcgBuAC4AawBtAGQALgBkAGsAAwAwAEsATQBEAEUAWABDADIAMAAwADYALgBpAG4AdABlAHIAbgAuAGsAbQBkAC4AZABrAAUAGgBpAG4AdABlAHIAbgAuAGsAbQBkAC4AZABrAAcACAA8TRLTaRLSAQAAAAA=
    C: TlRMTVNTUAADAAAAGAAYAFYAAAAYABgAbgAAAAAAAABIAAAADgAOAEgAAAAAAAAAVgAAAAAAAACGAAAAAYIIAgoAWikAAAAPaQBvAG0AdABlAHMAdAA6stwc8LXYfAAAAAAAAAAAAAAAAAAAAAC5L0RCGGq9hCGwriyfURwGscjNCDTBeSA=
    S: A00000001 NO AUTHENTICATE failed.
    C: A00000002 AUTHENTICATE PLAIN
    S: +
    C: XXXXXXXXXX
    S: A00000002 NO AUTHENTICATE failed.
    C: A00000003 LOGIN xxxxxxxxx xxxxxxxx
    S: A00000003 NO Server Unavailable. 15
    S: * BYE Connection closed. 14A

Exception:

    AuthenticationException: “Authentication failed.”
    at MailKit.Net.Imap.ImapClient.Authenticate(Encoding encoding,    ICredentials credentials, CancellationToken cancellationToken)
       at MailKit.MailService.Authenticate(String userName, String password,    CancellationToken cancellationToken)

回答1:

There is/was a bug in AUTHENTICATE NTLM authentication which is the first authentication mechanism that MailKit tried in your case. Unfortunately, there's also a bug(?) in Exchange in that when NTLM auth fails, it automatically seems to deny AUTHENTICATE PLAIN which is what MailKit falls back to trying.

As a workaround, disable NTLM authentication in MailKit by doing this:

client.AuthenticationMechanisms.Remove ("NTLM");

The resulting code should look like this:

using (var client = new ImapClient(new ProtocolLogger("imap.log")))
{
    try
    {
        client.Connect(server, this.port, true);
        client.AuthenticationMechanisms.Remove("XOAUTH2");
        client.AuthenticationMechanisms.Remove("NTLM");
        client.Authenticate(user, password);
        var inbox = client.Inbox;
        inbox.Open(FolderAccess.ReadWrite);

        if (inbox.Count > 0)
        {
            var range = Enumerable.Range(0, inbox.Count).ToArray();
            inbox.AddFlags(range, MessageFlags.Deleted, false);
            inbox.Expunge();
        }
        client.Disconnect(true);
    }
    catch (AuthenticationException e)
    {
        throw e;
    }
}


标签: c# imap mailkit