SocketException: No such host is known - Failed to

2019-06-14 15:17发布

问题:

public void EmailSending() {
    MailMessage mail = new MailMessage();

    mail.From = new MailAddress("xxx@gmail.com");
    mail.To.Add("xxx@gmail.com");
    mail.Subject = "Test Mail";
    mail.Body = "This is for testing SMTP mail from GMAIL";

    SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
    smtpServer.Port = 587;
    smtpServer.Credentials = new System.Net.NetworkCredential("xxx@gmail.com", "pw") as ICredentialsByHost;
    smtpServer.EnableSsl = true;
    ServicePointManager.ServerCertificateValidationCallback =
        delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        { return true; };
    smtpServer.Send(mail);
    Debug.Log("success");
}

I am getting this error

SocketException: No such host is known.

System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist)

回答1:

if you have sending from anroid device then I think you have same problem some time, make sure your android build asks for Internet Access permission.

Go to Android Player Settings, "Other settings" and look for Internet Access. It's "Auto" by default, set it to "Require".



回答2:

The error

SocketException: No such host is known.

System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist)

was occurring due to proxy server. My IP was behind a proxy server, after assigning the direct internet access the code is working fine.



标签: c# unity3d smtp