Is it possible to reset the ServicePointManager?

前端 未结 2 472
野的像风
野的像风 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.

提交回复
热议问题