System.Net.Mail.SMTPClient如何进行本地IP绑定 [英] How does System.Net.Mail.SMTPClient do its local IP binding

查看:164
本文介绍了System.Net.Mail.SMTPClient如何进行本地IP绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个负载平衡(NLB)ASP.NET Web应用程序发送电子邮件。

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

服务器是双重的,带有外部面向和内部(防火墙后面)面对IP。邮件服务器位于防火墙后面。

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

我们一直在处理SMTPClient类抛出异常的问题,指出无法连接到SMTP服务器。

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

网络人员告诉我们,他们正在看到尝试从外部面对的IP地址(防火墙阻止)连接到SMTP服务器。

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)

从我对网络启用应用程序的(不可否认的补丁)知识我认为本地IP绑定将基于目的地决定,即如果路由表表示IP地址可以通过特定的NIC比这是从出生请求生成的IP。我错了吗?

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?

查看SmtpClient.ServicePoint我开始认为我们可能是,我们可以(应该)强制明确绑定到特定的IP?

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?

具体来说我一直在看

ServicePoint.BindIPEndPointDelegate属性该页面的
...

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


备注:某些负载均衡技术
要求客户端使用特定的
本地IP地址和端口号,
而不是IPAddress.Any(或
IPAddress.IPv6Any for Internet
协议版本6)和短暂的
端口。您的BindIPEndPointDelegate可以
满足此要求。

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?

推荐答案

你需要这样做...

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("me@me.com");
    msg.IsBodyHtml = false;
    msg.Subject = "Mail Test";
    msg.To.Add(new MailAddress("you@you.com"));

    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);
}

这篇关于System.Net.Mail.SMTPClient如何进行本地IP绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆