Problems sending e-mail Asp.net.mvc

Asked

Viewed 594 times

2

The SMTP server requires a secure connection or the client has not been authenticated?

[HttpPost]
public ActionResult Index(string login)
{
    try
    {
        var tbuscar = new UsuarioAplicacao();
        var retorno = tbuscar.ListarPorLogin(login);

        if (retorno != null)
        {
            string infLogin = retorno.login;
            string infSenha = retorno.senha;
            string infNome = retorno.nomeusuario;
            int infId = retorno.idusuario;

            string texto = "O seu login é : " + infLogin + " é sua senha é : " + infSenha;

            SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com", 587);
            smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "senha");
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            MailMessage m = new MailMessage();
            m.Subject = "Recuperação de senha";
            m.Body = "Segue informações sobre usuário e senha: \n" + texto;
            m.To.Add(infLogin);
            m.From = new MailAddress("[email protected]");
            m.IsBodyHtml = true;
            smtp.Send(m);

            TempData["msg"] = "E-mail enviado com sucesso";    
        }
        else
        {
            TempData["Erro"] = "Usuário não cadastrado.";
        }

        ModelState.Clear();
    }
    catch (Exception ex)
    {
        TempData["Erro"] =  ex.Message;
    }

    return View();
}
  • Maybe you are using the real Gmail password, Google does not allow this type of operation. Here’s how to create a password for less secure apps: https://support.google.com/accounts/answer/6010255

  • This code of yours would work with yahoo, I have an application running that almost equal to your code. If google is not a must for you try with yahoo.

  • @Ricardo, I’ll do a test using yahoo

  • What is the error? Here in the codeproject you have a good example: http://www.codeproject.com/Tips/520998/Send-Email-from-Yahoo-GMail-Hotmail-Csharp

1 answer

1

With yahoo I’d be like this:

SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com", 587);
smtp.Credentials = new System.Net.NetworkCredential
      ("[email protected]", "senha");
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

MailMessage m= new MailMessage();
m.Subject = "Assunto do email";
m.Body = "corpo do email";
m.To.Add("[email protected]");
m.From = new MailAddress("[email protected]");
m.IsBodyHtml = true;

smtp.Send(m);
  • @Scratch :The SMTP server requires a secure connection or the client has not been authenticated. The server response was: 5.7.1 Authentication required

  • I’m using windows 8.1

  • Did you test the code exactly like this? Are there any different security settings in this email? My code runs as it is there. I went through this mistake when I was doing and I arrived at this result.

  • I changed the code of the question, look there, I appreciate the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.