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
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.
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;