tags:

views:

85

answers:

1

We have a load balanced (NLB) ASP.NET web app which sends email.

The servers are dual homed with an external facing and internal (behind firewall) facing IP. The Mail server is behind the firewall.

We have been seing a problem where the SMTPClient class throws an exception stating it is unable to connect to the SMTP server.

The networking guys are telling us they are seeing attempts to connect to the SMTP server from the external facing IP address (which the firewall is blocking)

From my (admittedly patchy) knowledge of network enabled applications I thought that the local IP binding would be decided based on the destination, i.e. if the routing tables say the IP address can be accessed through a particular NIC than that is the IP the outbound request is generated from. Am I wrong?

looking at SmtpClient.ServicePoint I'm beginning to think that we might be and that we can (should) force an explicit binding to a particular IP?

specifically I've been looking at
ServicePoint.BindIPEndPointDelegate Property from that page...

Remarks :Some load balancing techniques require a client to use a specific local IP address and port number, rather than IPAddress.Any (or IPAddress.IPv6Any for Internet Protocol Version 6) and an ephemeral port. Your BindIPEndPointDelegate can satisfy this requirement.

it just seems a little odd to me that I'd need to do that but perhaps thats common in this type of environment?

+1  A: 

You need to do something like this...

public delegate IPEndPoint BindIPEndPoint(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount);

private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount) {
    if (retryCount < 3 && ddSendFrom.SelectedValue.Length > 0)
        return new IPEndPoint(IPAddress.Parse("192.168.1.100"), 0); //bind to a specific ip address on your server
    else
        return new IPEndPoint(IPAddress.Any, 0);
}

protected void btnTestMail_Click(object sender, EventArgs e) {
    MailMessage msg = new MailMessage();
    msg.Body = "Email is working!";
    msg.From = new MailAddress("[email protected]");
    msg.IsBodyHtml = false;
    msg.Subject = "Mail Test";
    msg.To.Add(new MailAddress("[email protected]"));

    SmtpClient client = new SmtpClient();
    client.Host = "192.168.1.1";
    client.Port = 25;
    client.EnableSsl = false;
    client.ServicePoint.BindIPEndPointDelegate = new System.Net.BindIPEndPoint(BindIPEndPointCallback);
    client.Send(msg);
}
rushonerok