Is it possible to reset the ServicePointManager?

前端 未结 2 434
野的像风
野的像风 2021-01-22 10:07

I am attempting to following the code similar to the one given at How does System.Net.Mail.SMTPClient do its local IP binding I am using Windows 7 and .Net 4.0 on a machine with

相关标签:
2条回答
  • 2021-01-22 10:24

    The problem I faced in the question posted above was that all emails would be sent out using the IP of the first message. I think something (possibly the ServicePointManager) was caching the connection. While I have not found a solution to resetting the ServicePointManager, I realized my above attempt at setting client = null; does not really close the connection even if you call GC.Collect(); soon after. The only thing I found to work was:

    SmtpClient client = new SmtpClient();
    //Remaining code here.... 
    client.Send(msg); 
    client.Dispose(); //Secret Sauce
    

    Calling client.Dispose(); after sending each message always resets the connection, so the next message can choose which IP Address it needs to go out on.

    O. O.

    0 讨论(0)
  • 2021-01-22 10:33

    I ran into a similar problem and wanted to reset ServicePointManager and changing certs for different outcomes of tests. The way that worked for me was to set MaxServicePointIdleTime to a low value, which would effectively reset it.

    ServicePointManager.MaxServicePointIdleTime = 1;
    
    0 讨论(0)
提交回复
热议问题