2012-04-25 5 views
1

How does System.Net.Mail.SMTPClient do its local IP binding에 제공된 것과 비슷한 코드를 따르려고합니다. 여러 IP 주소가있는 컴퓨터에서 Windows 7 및 .Net 4.0을 사용하고 있습니다. 나는ServicePointManager를 재설정 할 수 있습니까?

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 주소가 설정됩니다 BindIPEndPointDelegate,이 사용됩니다있다. 다음 번에이 코드가 호출되면 대리자는 결코 으로 호출되지 않습니다. 즉 첫 번째 IP 주소가 이후에 사용됩니다. 코드가 호출 될 때마다 위임 콜백이 호출되는 상황을 변경할 수 있습니까?

ServicePointManager (정적 클래스)이 대리자에 대한 첫 번째 호출의 결과를 캐시하고 있다고 생각합니다. 이 클래스를 재설정 할 수 있습니까? 나는 성능에 신경 쓰지 않는다.

는 O. O.

답변

0

내가 위에서 게시 된 모든 이메일이 첫 번째 메시지의 IP를 사용하여 전송되는 것이 었 문제에 직면 한 문제를 주셔서 감사합니다. 뭔가 (아마도 ServicePointManager)이 연결을 캐싱 중이라고 생각합니다. ServicePointManager을 재설정하는 해결책을 찾지 못했지만 client = null; 설정시 위의 시도가 GC.Collect(); 후에 곧 연결을 닫지는 않는다는 것을 깨달았습니다.

SmtpClient client = new SmtpClient(); 
//Remaining code here.... 
client.Send(msg); 
client.Dispose(); //Secret Sauce 

는 항상 각 메시지를 전송 한 후 client.Dispose(); 호출 연결을 다시 때문에 다음 메시지는 IP 그것이 밖으로 갈 필요 주소를 선택할 수 있습니다 : 나는 일에 발견하는 유일한 것은이었다.

O. O.

+0

smtp 클라이언트를 사용하여 랩핑하고 사용할 수 있습니다. '(var client = new SmtpClient())를 사용하여 {client.Send (msg); }' –

1

나는 비슷한 문제로 실행 및 테스트의 다른 결과에 대한 ServicePointManager 변화하는 인증서 표시를 재설정하고 싶었다. 저를 위해 일한 방법은 MaxServicePointIdleTime을 낮은 값으로 설정하여 효과적으로 재설정했습니다.

ServicePointManager.MaxServicePointIdleTime = 1; 
관련 문제