0
Answer

BindIPEndPoint Delegate with SmtpClient causes endless loop

So I'm trying to use the BindIPEndPoint when sending an email but its causing an endless loop of calling BindIPEndPointCallback. It's driving me nuts and I cant figured out the cause.
 
I created a new test project and used the same methods and tried doing a HttpWebRequest with BindIPEndPointCallBack and it worked properly... is the System.Net.Mail.ServicePoint.BindIPEndPointDelegate broken?! 
  1. private string servicePointIP;  
  2. public string ServicePointIP  
  3. {  
  4.     get { return servicePointIP; }  
  5.     set { servicePointIP = value; }  
  6. }  
  7. public delegate IPEndPoint BindIPEndPoint(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount);  
  8. private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)  
  9. {  
  10.     Console.WriteLine("BindIPEndPoint Called");  
  11.     if (retryCount < 3 && servicePointIP != null)  
  12.         return new IPEndPoint(IPAddress.Parse(servicePointIP), 0);  
  13.     else  
  14.         return new IPEndPoint(IPAddress.Any, 0);  
  15. }  
  16.   
  17. private string SendMail(MailMessage msg, IPAddress ip)  
  18. {  
  19.     servicePointIP = "192.168.1.9";  
  20.     SmtpClient smtp = new SmtpClient();  
  21.     smtp.Host = "localhost";  
  22.     smtp.Port = 25;  
  23.     smtp.ServicePoint.BindIPEndPointDelegate = new System.Net.BindIPEndPoint(BindIPEndPointCallback);  
  24.     smtp.Send(msg);  
  25. }