问题描述
我正在尝试使用MimeKit和MailKit从收件箱中提取电子邮件.
但是登录到邮件服务器Imap无效.
我正在将993端口与ssl一起使用.
我尝试使用Telnet在端口143上进行连接/登录/FETCH,并且它可以正常工作.
我尝试使用Openssl在993端口上连接/登录/FETCH,并且它可以正常工作.
Mailkit v.1.4.2.1
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
例外:
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
身份验证中,这是MailKit针对您的情况尝试的第一种身份验证机制.不幸的是,Exchange中还存在一个 错误(?),当NTLM身份验证失败时,它会自动拒绝AUTHENTICATE PLAIN
,这是MailKit不能尝试的.
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.
作为一种解决方法,通过执行以下操作在MailKit中禁用NTLM
身份验证:
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;
}
}
这篇关于Mailkit身份验证到Imap失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!