问题
Hello and thank you for taking the time to look help me with my problem.
I have been working on a small webgl thing within Unity that will send the user an email after they have done some stuff. I got it all built and ready to go, however when I built it and went to test it I get the socketexception error, when sending the email.
Here is the code I am using to send the email
MailMessage mail = new MailMessage();
mail.From = new MailAddress(myEmail);
mail.To.Add(userEmail.text);
mail.Subject = "Thanks for viewing the Webcam page at WilliamLeonSmith.com";
mail.Body = "This is an automated email to deliver the attached images that were created at WilliamLeonSmith.com";
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential(myEmail, pass) as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpServer.Send(mail);
回答1:
You cannot use Sockets or any other .Net network class on WebGL. This is for security reasons. Although, you can use Unity's WWW
class.
You still have two Other Options.
[EASY]
1. Re-write the code you have in your question in php. Use Unity WWW
class to send message(toEmail, Subject, Body) to the php script. The php script
will then send the actually email with the information it received from the Unity App.
If you don't want to learn php,you can compile the code in your question as a console app and then use it as cgi instead of php. WWW
class can communicate with both php or cgi programs through GET
or POST
request.
[HARD]
2. You can implement smtp protocol or your own MailMessage
class with UnitySocketIO library. https://github.com/NetEase/UnitySocketIO
回答2:
Due to security implications, JavaScript code does not have direct access to IP Sockets to implement network connectivity. As a result, the .NET networking classes (ie, everything in the System.Net namespace, particularly System.Net.Sockets) are non-functional in WebGL.
According to unity Doc http://docs.unity3d.com/Manual/webgl-networking.html
Hence the Error.
来源:https://stackoverflow.com/questions/36168828/getting-system-net-sockets-socketexception-no-such-host-is-known-in-unity-webg