0 votes
by (200 points)
edited

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
by (58.9k points)
edited

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?

by (200 points)
edited

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
by (58.9k points)
edited

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.

by (200 points)
edited

Thanks Thomas! I'll give it a shot.

by (200 points)
edited

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);

by (58.9k points)
edited

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

by (200 points)
edited

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?

by (58.9k points)
edited

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")));
by (58.9k points)
edited

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.

by (200 points)
edited

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

...