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:
    143
    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

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

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143

    No, it's all test.

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

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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:
    143

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

    Nixel2013

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

    Los puertos estaban mal. Gracias igual
     
    JeffDUnity3D likes this.
  7. Rayzngames

    Rayzngames

    Joined:
    May 13, 2022
    Posts:
    1
    Hola disculpa estoy recibiendo el mismo mensaje de error,
    Al enviar un mail. en este caso me marca. System.IO.Ports: Connection closed. tengo entendido que el puerto de salida de mi dominio de mailing asignado a esta cuenta de mail no me deja salir salta el error puesto a que ese puerto no debe de estar abierto o habilitado, Lo malo es que el puerto de salida establecido me lo han dado hecho, por lo tanto no puedo cambiar nada..
    upload_2022-7-12_12-33-18.png
    Me pondré en contactó con mi proveedor de mailing en cuanto pueda. si os pasa algo similar mirar por los puertos de salida

    Full error log
     
  8. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    Pasa tu codigo para ver el error mas claramente
     
  9. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Never ever store credentials in a client (game, app...). Send your email from a (web) server.