开发者

Send Email via C# through my LAN interface

开发者 https://www.devze.com 2023-02-10 09:15 出处:网络
I am trying to simulate some ad hoc network, I use my wireless card to connect to the ad hoc network, I want to be able to mail myself some specific messages when I get some trigger from this ad hoc n

I am trying to simulate some ad hoc network, I use my wireless card to connect to the ad hoc network, I want to be able to mail myself some specific messages when I get some trigger from this ad hoc network. my laptop connected to internet via Lan interface and connect to ad hoc network via wireless card.

this id my code:

开发者_如何学运维 private void send_mail()
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add("XXXXX@YYYY.com");
            message.Subject = "test";
            message.From = new System.Net.Mail.MailAddress("ZZZZZ@MMMMM.com");
            message.Body = "This is the message body";
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("a.b.c.d");
            smtp.Send(message);
        }

I want to know How can I set interface for sending mail. by default the code want use my wireless card to send mail and can't connect to smtp server and I got error.

I want my send_mail() function connect through LAN interface not wireless interface.


It's an IP routing problem. You need to setup routes on your computer to direct access to a.b.c.d through the WiFi interface.

Open a command prompt with Admin privileges and use the route add command.


Set your LAN interface to be your default gateway, or set your LAN interface to be your default connection/internet connection.

The operating system has to know to use the LAN interface first for internet connections.


one option that comes to mind is to block port 25 on the port you dont want to send though... Is the Ad-Hoc network connected to an Internet Connection? if not, the email should go though your Wired Network connection anyway....


That's outside of the scope of the SmtpClient it's pretty much as if you try to resolv an adress using the command promt and do a ping or lookup, it will use the interface that can reach that address.

Also, be sure that the relay you are using is using port 25. You can try telnet to it first, if that works you should be able to connect to it using SmtpClient.


It is impossible to choose the network card to use. From within .NET you cannot choose which network card is used to open a socket for network communication.


I think it's an issue with how the network is set up on your PC, so it might be worth asking about that on superuser.com.

However, if nothing else works, you could temporarily disable your wireless connection before sending the email and then enable it again afterwards. Assuming that you're using DHCP, you can do this via WMI, the top sample on this page shows how you can disable a network connection using WMI?. If you're not using DHCP there's still ways of doing this but it's a bit more difficult.

If you haven't used WMI in C# before, this article might be a good starting point: WMI Made Easy For C#

Or taking Serge's answer as a starting point, you can do Route add programmatically via CreateIpForwardEntry and later delete it via DeleteIpForwardEntry.

0

精彩评论

暂无评论...
验证码 换一张
取 消