【发布时间】:2016-07-20 00:58:55
【问题描述】:
我正在尝试遵循与How does System.Net.Mail.SMTPClient do its local IP binding 中给出的代码类似的代码,我在具有多个 IP 地址的机器上使用 Windows 7 和 .Net 4.0。我定义了BindIPEndPointDelegate
private static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
string IPAddr = //some logic to return a different IP each time
return new IPEndPoint(IPAddr, 0);
}
然后我使用
发送我的电子邮件SmtpClient client = new SmtpClient();
client.Host = SMTP_SERVER; //IP Address as string
client.Port = 25;
client.EnableSsl = false;
client.ServicePoint.BindIPEndPointDelegate
= new System.Net.BindIPEndPoint(BindIPEndPointCallback);
client.ServicePoint.ConnectionLeaseTimeout = 0;
client.Send(msg); //msg is of type MailMessage properly initialized/set
client = null;
第一次调用此代码时,将调用委托,并且无论设置什么 IP 地址,都会使用它。随后调用此代码时,委托永远不会被调用 ,即随后使用第一个 IP 地址。 是否可以改变这种每次调用代码时调用委托回调的情况?
我认为ServicePointManager(这是一个静态类)缓存了第一次调用委托的结果。可以重置这个类吗?我不在乎性能。
谢谢你, O.O.
【问题讨论】:
标签: c# .net sockets email network-programming