Search Unity

IOException: Connection closed ------- Send Mail with unity

Discussion in 'Scripting' started by Nixel2013, Dec 16, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    62
    Error when pressing the button with the StartCoroutine:

    IOException: Connection closed
    System.Net.Mail.SmtpClient.Read () (at <9f8c4786222746238eb929db40bf8dd1>:0)
    System.Net.Mail.SmtpClient.SendCore (System.Net.Mail.MailMessage message) (at <9f8c4786222746238eb929db40bf8dd1>:0)
    System.Net.Mail.SmtpClient.SendInternal (System.Net.Mail.MailMessage message) (at <9f8c4786222746238eb929db40bf8dd1>:0)
    System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) (at <9f8c4786222746238eb929db40bf8dd1>:0)
    Rethrow as SmtpException: Message could not be sent.
    System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) (at <9f8c4786222746238eb929db40bf8dd1>:0)
    Correo+<EnviarDatos>d__5.MoveNext () (at Assets/ComponentesMenu/PanelRegistro/Correo.cs:51)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using System.Net;
    7. using System.Net.Mail;
    8. using System.Net.Security;
    9. using System.Security.Cryptography.X509Certificates;
    10.  
    11. public class Correo : MonoBehaviour
    12. {
    13.     public TMP_InputField NombreDeUsuario;
    14.     public TMP_InputField CorreoElectronico;
    15.     public TMP_InputField Contraseña;
    16.     //public TMP_InputField Path;
    17.     public TextMeshProUGUI error;
    18.  
    19.     public void SendMail()
    20.     {
    21.         StartCoroutine(EnviarDatos());
    22.     }
    23.  
    24.     public IEnumerator EnviarDatos()
    25.     {
    26.         yield return new WaitForSeconds(0.0f);
    27.         if (NombreDeUsuario.text == "" || CorreoElectronico.text == "" || Contraseña.text == "")
    28.         {
    29.             error.text = "Por favor, Complete todos los campos";
    30.             //Debug.Log("Por favor, Complete todos los campos");
    31.         }
    32.         else
    33.         {
    34.             MailMessage mail = new MailMessage();
    35.  
    36.             mail.From = new MailAddress(CorreoElectronico.text);
    37.             mail.To.Add("rplatinoamericasur@gmail.com");
    38.             mail.Subject = NombreDeUsuario.text;
    39.             mail.Body = "Mail From :" + CorreoElectronico + "/n ------------------------- /n /n" + Contraseña.text;
    40.             /*if (Path.text != "")
    41.             {
    42.                 Attachment attachment = null;
    43.                 attachment = new Attachment(Path.text);
    44.                 mail.Attachments.Add(attachment);
    45.             }*/
    46.             SmtpClient smtpServer = new SmtpClient("imap.gmail.com");
    47.             smtpServer.Port = 993;
    48.             smtpServer.Credentials = new System.Net.NetworkCredential("CorreoElectronico", "Password");// as ICredentialsByHost;
    49.             smtpServer.EnableSsl = true;
    50.             ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
    51.             smtpServer.Send(mail);
    52.             error.text = "Correo mandado con exito";
    53.  
    54.         }
    55.     }
    56. }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,678
    I trust that the password you are using isn't actually "Password", correct?
     
  3. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    62

    No, it's all test.

    But I don't understand how this type of code works, so I ask for help
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    6,678
    What do you mean it's all test, are you using your correct GMail password?
     
    Nixel2013 likes this.
  5. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    62

    I tried anyway, with true data and with false data, and still I get that error
     
  6. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    62
    Problema resuelto.

    Los puertos estaban mal. Gracias igual
     
    JeffDUnity3D likes this.
unityunity