如果使用
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
和
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;
}
它應該返回類似
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)
這可以簡單地修改返回地址你需要
@mathieu是否可以同時開發和部署系統?另外我如何現在初始化SmtpClient類。 – Deeptechtons 2011-06-09 08:20:48
你可以使用特定的配置文件進行生產,如果smtp主機與開發不同 – mathieu 2011-06-09 08:25:42
@mathieu我無法得到你。以上設置僅適用於localhost,這就是你的意思? – Deeptechtons 2011-06-09 08:27:16