我试图遵循类似于在System.Net.Mail.SMTPClient如何进行本地IP绑定?上给出的代码,我在一台具有多个IP地址的机器上使用了Windows7和.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.
发布于 2016-07-19 16:58:55
我遇到了一个类似的问题,我想重新设置ServicePointManager,并为不同的测试结果更改证书。我的工作方式是将MaxServicePointIdleTime设置为一个低值,这将有效地重置它。
ServicePointManager.MaxServicePointIdleTime = 1;发布于 2012-05-04 21:08:03
我在上面发布的问题中遇到的问题是,所有的电子邮件都将使用第一条消息的IP发送出去。我认为有些东西(可能是ServicePointManager__)在缓存连接)。虽然我还没有找到重置ServicePointManager,的解决方案,但我意识到,即使在不久之后调用GC.Collect();,我的上述设置client = null;的尝试也不会真正关闭连接。我发现唯一有用的东西是:
SmtpClient client = new SmtpClient();
//Remaining code here....
client.Send(msg);
client.Dispose(); //Secret Sauce发送每条消息后调用client.Dispose();总是重置连接,因此下一条消息可以选择它需要在哪个IP地址上运行。
O.
https://stackoverflow.com/questions/10320992
复制相似问题