2

How can i get the LocalhostName, Ip of the machine hosting the Application. For development it would be localhost for deployment something different. This i need to initialize the SmtpClient to send emails through application

SmtpClient emailClient = new SmtpClient("host","port");//port is optional

i am looking for a permanent solution, no workarounds and no sniffing from response, request and this could be spoofed[hope i am not crazy because no one can spoof the servers data in headers can they?]

Deeptechtons
  • 10,945
  • 27
  • 96
  • 178

2 Answers2

3

If you want to configure SmtpClient class, you should have a look at the system.net > mailsettings entry of the web.config : http://msdn.microsoft.com/en-us/library/w355a94k.aspx

<configuration>
    <system.net>
        <mailSettings>
            <smtp deliveryMethod="network">
                <network
                    host="localhost"
                    port="25"
                    defaultCredentials="true"
                />
            </smtp>
        </mailSettings>
    </system.net>
</configuration>

And instanciate the StmpClient with parameterless constructor

var client = new SmtpClient();
mathieu
  • 30,974
  • 4
  • 64
  • 90
  • @mathieu does this work both development and deployment systems? also how do i initialize the SmtpClient class now. – Deeptechtons Jun 09 '11 at 08:20
  • You can use specific config file for production, if smtp host is different from development – mathieu Jun 09 '11 at 08:25
  • @mathieu i couldn't get you. Above settings work only for localhost that's what you mean? – Deeptechtons Jun 09 '11 at 08:27
  • Above settings means that the smtp server is located on the host "localhost". If server is on "stmp.mycompany.com" you have to edit the configuration accordingly. – mathieu Jun 09 '11 at 08:29
  • If the stmp server is on the same server as your application, settings should work, as "localhost" represents the local machine... – mathieu Jun 09 '11 at 08:30
  • @mathieu got it, but just a confusion when an asp.net application is hosted in development PC[some hosting company] then localhost to that application means the development system right, then what is the reason behind changing hostname there again? – Deeptechtons Jun 09 '11 at 08:34
  • If you want to communicate with a service located on current machine, you just use localhost. – mathieu Jun 09 '11 at 08:37
0

if you use

    IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

and

public static bool IsLocalIpAddress(string host)
{
  try
  { // get host IP addresses
    IPAddress[] hostIPs = Dns.GetHostAddresses(host);
    // get local IP addresses
    IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

    // test if any host IP equals to any local IP or to localhost
    foreach (IPAddress hostIP in hostIPs)
    {
      // is localhost
      if (IPAddress.IsLoopback(hostIP)) return true;
      // is local address
      foreach (IPAddress localIP in localIPs)
      {
        if (hostIP.Equals(localIP)) return true;
      }
    }
  }
  catch { }
  return false;
}

it should return something like

IsLocalIpAddress("localhost");        // true (loopback name)
IsLocalIpAddress("127.0.0.1");        // true (loopback IP)
IsLocalIpAddress("MyNotebook");       // true (my computer name)
IsLocalIpAddress("192.168.0.1");      // true (my IP)
IsLocalIpAddress("NonExistingName");  // false (non existing computer name)
IsLocalIpAddress("99.0.0.1");         // false (non existing IP in my net)

this can be simply modified to return the address you need

harryovers
  • 3,087
  • 2
  • 34
  • 56
  • All i wanted to know is was there any way to get the IP, HostName of the machine hosting the asp.net application. And i get a method where i need to enter each host and check for valid host?? – Deeptechtons Jun 09 '11 at 08:32
  • @Deeptechons the first part will return all the machines ip addresses, you just need to find out which one of the ip addresses to use as some of them will be local addresses and others won't – harryovers Jun 09 '11 at 09:29