+1 vote

I have a server with multiple IP addresses. How can I choose an specific IP address to be used by mail.dll when connecting to a remote smtp server?

by (600 points)
retagged by

1 Answer

0 votes

I'm not sure if I understand you correctly, but you can use Smtp.Connect method with IP address as parameter:

using(Smtp smtp = new Smtp())
{
    smtp.Connect("192.168.0.1");  // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password");

    //...

    smtp.Close();
}
by (297k points)
This is not what I meant. I need to specify the source IP address, not the destination.

Say my server has 2 IPs, 100.100.100.1 and 200.200.200.1, and I need my outgoing mail to always originate from 200.200.200.1.

From what I'm seeing, the ClientBase exposes the socket, so I should be able to bind the endpoint before connecting, like:

    using (var smtp = new Smtp()) {
        smtp.Socket.Bind(myOutgoingEndpoint);
        smtp.Connect(targetMX);
        ...
    }

Edit: This works as expected, the outgoing mail comes from the specified endpoint.

Thanks.
...