gpt4 book ai didi

c# - Mailkit 对 Imap 的身份验证失败

转载 作者:行者123 更新时间:2023-11-30 21:46:02 26 4
gpt4 key购买 nike

我正在尝试使用 MimeKit 和 MailKit 从收件箱中获取电子邮件。
但是无法登录邮件服务器Imap。
我将端口 993 与 ssl 结合使用。
我尝试使用 Telnet 在端口 143 上连接/LOGIN/FETCH,它工作正常。
我尝试使用 Openssl 在端口 993 上连接/LOGIN/FETCH 并且它有效。
Mailkit v.1.4.2.1

科德:

     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)

最佳答案

有/曾经有一个bugAUTHENTICATE NTLM 身份验证中,这是 MailKit 在您的案例中尝试的第一个身份验证机制。不幸的是,Exchange 中还有一个错误(?),当 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com