c# - 由于意外的数据包格式登录到 Pop3,握手失败

标签 c# ssl pop3

我正在尝试更新监控 Exchange Server 邮箱的代码,以便它可以使用 SSL 登录。但是,当它尝试登录时,我收到错误消息:由于意外的数据包格式,握手失败。

我的连接代码如下(删除了日志记录和评论):

ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;

try
{
    tcpClient = new TcpClient(Host, Port);
}
catch (SocketException e) { ... }

try
{
    var stream = client.GetStream();
    ssl = new SslStream(stream);
    ssl.AuthenticateAsClient(Host, null, SslProtocols.Tls, true);
    streamReader = new StreamReader(ssl);

    response = streamReader.ReadLine();
    if (response.StartsWith("+OK"))
    {
        response = SendReceive("USER ", UserName.Trim() + "@" + Domain.Trim());
        if (response.StartsWith("+OK"))
        {
            response = SendReceive("PASS ", Password);
        }
    }

    if (response.StartsWith("+OK"))
        result = true;
}
catch (Exception e) { ... }

根据日志记录,我已将异常精确定位到第二次 Try 后前 4 行中的某处(实际上是在 GetStreamReader 方法中,但为了简洁和可读性,我将其合并)。

我收到的异常如下:

    --- Exception ---
Error:  3/8/2016 10:32:35 AM    The handshake failed due to an unexpected packet format.
Info:   3/8/2016 10:32:35 AM    There was an error connecting to the account.
The server returned 
The handshake failed due to an unexpected packet format.
Info:   3/8/2016 10:32:35 AM    Source: System
Info:   3/8/2016 10:32:35 AM    Method: Void StartReadFrame(Byte[], Int32, System.Net.AsyncProtocolRequest)
Info:   3/8/2016 10:32:35 AM    
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)
   at POP3_Communication.Pop3.GetStreamReader(TcpClient client)
   at POP3_Communication.Pop3.Connect()

最佳答案

我可以根据您的异常堆栈跟踪进一步缩小范围:)

at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)

问题是您正在连接到未启用 SSL 的端口(即您可能连接到端口 110 而不是 995)。

您是否考虑过使用类似 MailKit 的库?而不是推出自己的解决方案?

关于c# - 由于意外的数据包格式登录到 Pop3,握手失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875039/

相关文章:

openssl - TLS/SSL 中的消息完整性

pop3 - 如何使用 POP3 协议(protocol)仅检索新电子邮件

ruby-on-rails - RoR 应用程序中的 "getaddrinfo: Temporary failure in name resolution"

internet-explorer - 有没有办法检查是否启用了 TLS?

java - 如何确定 SMTP 和 POP 响应的结束?

c# - 无法在默认 MVC 应用程序中存储用户

c# - .NET 中 yield 和 await 如何实现控制流?

c# - 如何根据正则表达式分割字符串?

c# - 如何将字符串扩展应用于 Func<String>

Magento 始终在页面上显示所有 SSL url