我正在尝试使用MimeKit和MailKit从收件箱中提取电子邮件。
但是登录到邮件服务器Imap无效。
我将993端口与ssl一起使用。
我尝试使用Telnet在端口143上连接/登录/ FETCH,并且它可以工作。
我试图用Openssl在993端口上连接/登录/ FETCH,它可以工作。
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


例外:

    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)

最佳答案

AUTHENTICATE NTLM身份验证中有一个bug,这是MailKit在您的情况下尝试的第一种身份验证机制。不幸的是,Exchange中还有一个bug(?),当NTLM身份验证失败时,它似乎自动拒绝了AUTHENTICATE PLAIN,这是MailKit不能尝试的。

解决方法是,通过执行以下操作在MailKit中禁用NTLM身份验证:

client.AuthenticationMechanisms.Remove ("NTLM");


产生的代码应如下所示:

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# - Mailkit身份验证到Imap失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39573233/

10-14 17:26