You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用EWS读取Exchange Server邮件时遇连接关闭错误求助

Fixing "Underlying Connection Closed" Error with EWS Exchange 2013

Hey there, let's troubleshoot that frustrating connection error you're seeing when using EWS to read emails. That "request failed. underlying connection closed: unexpected error on receive" message typically stems from TLS mismatches, conflicting connection settings, or inefficient request patterns. Let's go through your code and fix this step by step:

1. Enforce TLS 1.2 (Critical for Exchange 2013)

Exchange 2013 requires TLS 1.2 for secure connections, but older .NET frameworks (pre-4.7) don't enable this by default. Add this line right at the start of your try block to force TLS 1.2:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Without this, your app might try to use outdated TLS versions (1.0/1.1) that are disabled on modern Exchange servers, causing the connection to drop mid-handshake.

2. Resolve Autodiscover vs Manual URL Conflict

You're calling AutodiscoverUrl and then immediately overwriting the exchange.Url manually—this creates conflicting connection state. Pick one approach:

  • Use Autodiscover (Recommended): Remove the manual URL assignment, since Autodiscover will automatically find the correct EWS endpoint:
    exchange.AutodiscoverUrl("mail-id", RedirectionUrlValidationCallback);
    // Delete the line: exchange.Url = new System.Uri("https://URL/EWS/Exchange.asmx");
    
  • Stick with Manual URL: Skip Autodiscover entirely to avoid connection context confusion:
    // Delete the line: exchange.AutodiscoverUrl("mail-id", RedirectionUrlValidationCallback);
    exchange.Url = new System.Uri("https://URL/EWS/Exchange.asmx");
    

3. Enable KeepAlive for Persistent Connections

You set exchange.KeepAlive = false—this forces a new connection for every request (like each email bind), which can trigger server connection limits or timeouts. Change this to:

exchange.KeepAlive = true;

Keeping the connection alive reduces overhead and prevents unexpected closures from frequent connection re-establishment.

4. Verify Certificate Validation Callback

Make sure your CertificateValidationCallBack is correctly accepting the Exchange server's certificate. If it's rejecting valid certificates (e.g., self-signed ones in a test environment), the TLS handshake will fail and close the connection. For testing purposes, you can use a permissive callback (never use this in production):

private static bool CertificateValidationCallBack(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    // Accept all certificates for testing only
    return true;
}

In production, implement proper certificate validation to avoid security risks.

5. Optimize Redundant Item Binding

Your code has unnecessary double-binding for email items:

EmailMessage message2 = EmailMessage.Bind(exchange, (EmailMessage.Bind(exchange, item.Id)).Id, ...);

This calls Bind twice for the same item, creating extra requests that can overwhelm the connection pool. Simplify it to:

EmailMessage message2 = EmailMessage.Bind(exchange, item.Id, new PropertySet(BasePropertySet.FirstClassProperties, new ExtendedPropertyDefinition(0x1013, MapiPropertyType.Binary)));

This reduces request load and lowers the chance of throttling or connection closures.

Final Notes

After applying these changes, keep TraceEnabled (which you already have) turned on—detailed EWS logs will help you pinpoint if the connection is failing during TLS handshake, autodiscover, or item fetching.

内容的提问来源于stack exchange,提问作者marwen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:36:17