Unable to Send Mail [duplicate]

0

Good this is my code:

 MailMessage msg = new MailMessage();
 msg.To.Add(new MailAddress("[email protected]"));
 msg.From = new MailAddress("[email protected]");
 msg.Subject = "Asunto(Correo Prueba)";
 msg.Body = "Contenido Prueba";
 msg.IsBodyHtml = false;
 msg.Priority = MailPriority.Normal;



        SmtpClient smtpClient = new SmtpClient();
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.Port = 465;
        smtpClient.EnableSsl = true;
        smtpClient.Credentials = new NetworkCredential("[email protected]", "xxxxxx");

ServicePointManager.ServerCertificateValidationCallback =
                      (s, certificate, chai, sslPolicyErrors) => true;

        //Envio del correo
        try
        {
            //Envio del correo
            smtpClient.Send(msg);
            return "OK";
        }
        catch (SmtpException ex)
        {
            Console.WriteLine(ex.Message);
            return ex.Message;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return ex.Message;
        }
        finally
        {
            msg.Dispose();
            smtpClient.Dispose();

        }'

You do not send me the mail and I just get a message saying Error when sending mail, I put the email to allow access from less secure applications and also activate the imap that I may be missing?

    
asked by RoyMartinez 11.09.2018 в 20:14
source

2 answers

0

Try using port 587

SmtpClient smtp = new SmtpClient()
{
    Host = "smtp.gmail.com",
    Port = 587,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential("[email protected]", "password"),
    EnableSsl = true
};

MailMessage - Send mail with GMail (1/3)

    
answered by 11.09.2018 в 20:38
-1

Try this function:

 public static
                string EnviarCorreo(string destinatario, string asunto, string cuerpo,
                List<AlternateView> vistas = null)
            {
                try
                {

                    var m = new MailMessage(new MailAddress("[email protected]", "NAME"),
                        new MailAddress(destinatario))
                    {
                        Subject = asunto,
                        Body = cuerpo,
                        IsBodyHtml = true
                    };
                    if (vistas != null)
                    {
                        foreach (var item in vistas)
                        {
                            m.AlternateViews.Add(item);
                        }
                    }
                    var smtp = new SmtpClient("correo.algo.algomas")
                    {
                        Credentials = new NetworkCredential("[email protected]", "password"),
                        EnableSsl = false
                    };
                    smtp.Send(m);
                    return string.Empty;
                }
                catch (Exception exc)
                {
                    return exc.Message;
                }
            }
    
answered by 11.09.2018 в 20:42