0
Answer

Using BindIPEndPointDelegate with SmtpClient causes endless loop

ryan taylor

ryan taylor

14y
4.2k
1
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?!


private string servicePointIP;
public string ServicePointIP
{
    get { return servicePointIP; }
    set { servicePointIP = value; }
}
public delegate IPEndPoint BindIPEndPoint(ServicePoint servicePoint, 
    IPEndPoint remoteEndPoint, int retryCount);
private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, 
    IPEndPoint remoteEndPoint, int retryCount)
{
    Console.WriteLine("BindIPEndPoint Called");
    if (retryCount < 3 && servicePointIP != null)
        return new IPEndPoint(IPAddress.Parse(servicePointIP), 0);
    else
        return new IPEndPoint(IPAddress.Any, 0);
}
private string SendMail(MailMessage msg, IPAddress ip)
{
    servicePointIP = "192.168.1.9";
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "localhost";
    smtp.Port = 25;

    smtp.ServicePoint.BindIPEndPointDelegate = new System.Net.BindIPEndPoint(BindIPEndPointCallback);

    smtp.Send(msg);
}