SocketException: No such host is known - Failed to

2019-06-14 15:14发布

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)

标签: c# unity3d smtp
2条回答
何必那么认真
2楼-- · 2019-06-14 15:32

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".

查看更多
放我归山
3楼-- · 2019-06-14 15:39

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.

查看更多
登录 后发表回答