I recently had a question about aspNetEmail (thanks Clay!). He wanted to know how he could use aspNetEmail to choose different local IPs for sending emails.
A little background
aspNetEmail creates it's own TCP/IP sockets to send email. What it does, is open a local TCP/IP socket, and establish a remote endpoint with the server specified by the .Server property.
When that local TCP/IP socket is created, aspNetEmail lets the .NET framework randomly select a local IP and Port to use. We can override the behavior by using the .LocalEndpoint property found on the EmailMessage object.
Here is a short but complete code example that demonstrates this functionality.
C#
EmailMessage msg = new EmailMessage( "127.0.0.1" ); msg.FromAddress = "me@mycompany.com"; msg.To = "you@yourcompany.com"; msg.Subject = "Your Order Confirmation Number is 12345"; msg.Body = "put our body contents here..."; //local endpoint -- this opens a local TCP/IP socket on IP 192.168.1.18 and port 5555 msg.LocalEndPoint = new IPEndPoint( IPAddress.Parse( "192.168.1.18" ), 5555 ); //if we only know the IP we want the socket open on, and not the port, we can //pass in 0 for the port, and the underlying OS will randomly pick an open port //for example //msg.LocalEndPoint = new IPEndPoint( IPAddress.Parse( "192.168.1.18" ), 0); //any logging (not reqired) for troubleshooting msg.LogPath = "c:\\temp\\email.log"; msg.Logging = true; msg.Send();
VB.NET
Dim msg As New EmailMessage("127.0.0.1") msg.FromAddress = "me@mycompany.com" msg.To = "you@yourcompany.com" msg.Subject = "Your Order Confirmation Number is 12345" msg.Body = "put our body contents here..." 'local endpoint -- this opens a local TCP/IP socket on IP 192.168.1.18 and port 5555 msg.LocalEndPoint = New IPEndPoint(IPAddress.Parse("192.168.1.18"), 5555) 'if we only know the IP we want the socket open on, and not the port, we can 'pass in 0 for the port, and the underlying OS will randomly pick an open port 'for example 'msg.LocalEndPoint = New IPEndPoint(IPAddress.Parse("192.168.1.18"), 0) 'any logging (not reqired) for troubleshooting msg.LogPath = "c:\temp\email.log" msg.Logging = True msg.Send()
As always, if anyone has any questions, feel free to contact me over at Contact Us
Thanks!
Dave Wanta