How to specify the sender IP address for Rebex.Net.SMTP.Send

0 votes
asked Aug 28, 2013 by Gade Chua (200 points)
edited Jan 2, 2014

The receiving SMTP server in my environment that relays the email message coming from Rebex.Net.SMTP.Send method screens the incoming IP addresses. How do we specify a single IP of the server at the Send method? We have over 25 possible IPs coming from 5 different servers, enter all of them in the screening rule seems overkill.

Thanks!

Applies to: Rebex Secure Mail

2 Answers

0 votes
answered Aug 28, 2013 by Tomas Knopp (58,580 points)
edited Aug 28, 2013

Do you want to specify the IP address of the server to which the Rebex SMTP client connects, or the IP address from which the Rebex SMTP client connects?

commented Aug 28, 2013 by Gade Chua (200 points)
edited Aug 28, 2013

IP address from which Rebex SMTP connects. We need an origin IP address of our server (where our program that uses rebex smtp is running), so we can get the destination SMTP server to allow relaying of an email from our server. If that makes sense.

+1 vote
answered Aug 29, 2013 by Tomas Knopp (58,580 points)
edited Jan 2, 2014

You can instruct Rebex components (including Rebex Smtp) to use a custom socket factory which will directly say which IP address Rebex SMTP will use - just follow the steps from this answer.

Apart from the original question, the code for Rebex Smtp would look like this:

using Rebex.Net;
using System.Net;
...

Smtp smtp = new Smtp();
smtp.SetSocketFactory(SimpleSocket.GetFactory(IPAddress.Parse("127.0.0.1")));
smtp.Connect(hostname);

Please note, that you have to assign one of the IP addresses which are configured at the local machine' s network interface.

commented Aug 29, 2013 by Gade Chua (200 points)
edited Aug 29, 2013

Thanks Thomas! I'll give it a shot.

commented Aug 30, 2013 by Gade Chua (200 points)
edited Aug 30, 2013

At smtp.Connect(hostname), do you specify the local hostname (where you bind the socket) or it's the external SMTP server hostname? I kept getting "the requested address is not valid in its context" when try to connect to the external SMTP server using ip address. This is how my code looks like

Rebex.Net.Smtp rebexSMTP = new Rebex.Net.Smtp(); rebexSMTP.SetSocketFactory(SimpleSocket.GetFactory(IPAddress.Parse("<local ip="">"))); rebexSMTP.Connect("<external smtp="" ip="">, 25); rebexSMTP.Send(rebexMessage);

commented Aug 30, 2013 by Tomas Knopp (58,580 points)
edited Aug 30, 2013

hostname specifies the destination SMTP server name (e.g. smtp.gmail.com).

commented Aug 30, 2013 by Gade Chua (200 points)
edited Aug 30, 2013

Thanks Thomas! I originally use Rebex.Net.Smtp.Send(mailMessage, destIP, 25) without specifying binding IP address and the email came through fine. Now that I specified the binding IP address and try to use the code you recommended, I got an error "The request address is not valid in its context" at the line rebexSMTP.Connect(desIP, 25) with inner exception ...at System.Net.Sockets.DoBind(....). do you know what might have been the different in these 2 calls? Why one works but the other one doesn't?

commented Sep 2, 2013 by Tomas Knopp (58,580 points)
edited Sep 2, 2013

Please note that you have to assign a valid IP address which is properly registered on your local machine network interface. E.g. I have to use IP address 192.168.37.72 as my machine uses this IP to connect to the LAN network via the Ethernet adapter. You have to find out what is the IP address of your computer, then specify it yourself:

smtp.SetSocketFactory(SimpleSocket.GetFactory(IPAddress.Parse("my_valid_IP_address")));
commented Sep 3, 2013 by Tomas Knopp (58,580 points)
edited Jan 2, 2014

And to answer your question, if you use Smtp.Send static method, it internally creates an instance of Smtp, connects to the server at the specified address and then sends the email, so there is no tweak in this. However, when specifying a custom SocketFactory, you cannot use the static Send method, so that's why you have to instantiate the Smtp, and then do Connect.

commented Sep 3, 2013 by Gade Chua (200 points)
edited Sep 3, 2013

That works. Just verified and it was a bug on my side. Thanks Thomas!

...