Send email Login & Password C# windows Form

Asked

Viewed 1,060 times

2

I saw that I had to send e-mail when the user forgets the password and I put together different codes but all gave errors.

I think there’s something missing and I wonder if someone could help me?

follows the codes

 if (txtLogin.Text != "")
        {

            UsuarioDTO objUsuDto = new UsuarioDTO();
            objUsuDto.Login = txtLogin.Text;
            objUsuDto = new UsuarioModel().PesquisarUsuarioLogin(objUsuDto);
            emailUsuarioEnvio = objUsuDto.Email;
            login = objUsuDto.Login;
            senha = objUsuDto.Senha;

            if (emailUsuarioEnvio != "")
            {

// attempt n1

                //SmtpClient smtp = new SmtpClient("smtp.dominio.com.br", 587);
                //smtp.Credentials = new NetworkCredential("[email protected]","Senha");
                //smtp.EnableSsl = true;
                //MailAddress remetente = new MailAddress("[email protected]");
                //MailAddress destinatario = new MailAddress(emailUsuarioEnvio);


                //MailMessage mensagem = new MailMessage(remetente, destinatario);


                //mensagem.Body = "Seu Login é: "+login + "  Sua senha é: "+senha;
                //mensagem.Subject = "Recuperação de Senha do Sistema
                //NetworkCredential credenciais = new NetworkCredential("[email protected]", "Senha");
                //smtp.Credentials = credenciais;
                //smtp.Send(mensagem);

// another attempt N2

                //System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                //message.To.Add(emailUsuarioEnvio);
                //message.Subject = "Recuperação de senha do sistema de empilhadeira";
                //message.From = new System.Net.Mail.MailAddress("[email protected]");
                //message.Body = "\n O seu Login é: " + login + "\n A sua senha é: " + senha;
                //System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.dominio.com.br");
                //smtp.Send(message);

// attempt to N3

                SmtpClient cliente = new SmtpClient("mai.dominio.com.br");
                MailMessage Message = new MailMessage();
                Message.From = new MailAddress("[email protected]");
                Message.To.Add(emailUsuarioEnvio);
                Message.Body = "teste de email";
                Message.Subject = "Seja bem Vindo";
                cliente.Credentials = new System.Net.NetworkCredential("[email protected]", "Senha");
                cliente.Port = System.Convert.ToInt32(587);
                cliente.Send(Message);

            }

inserir a descrição da imagem aqui

2 answers

2

Would something like this:

try
        {
            MailMessage mensagem = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            mensagem.From = new MailAddress("[EMAIL AQUI]", "[NOME AQUI]");
            mensagem.To.Add("[EMAIL AQUI]");
            mensagem.Subject = ("[ASSUNTO AQUI]");
            mensagem.Priority = MailPriority.Normal;

            //Configuracao SMTP para HOTMAIL
            smtp.EnableSsl = true;
            smtp.Port = 587;
            smtp.Host = "smtp.live.com";
            smtp.Credentials = new System.Net.NetworkCredential("[EMAIL]", "[SENHA]");
            smtp.Send(mensagem);

        }

catch { }

Stay tuned for the information you should replace... There may be some changes to the SMTP configuration part depending on the server you choose to send.

0

There’s nothing wrong with your last code Dispose of SmtpClient. It is also not necessary to convert the number 587 to integer as it is already an integer. The code I used, copied from yours, and worked was:

using (SmtpClient cliente = new SmtpClient("usuario.dominio.com"))
{
    MailMessage Message = new MailMessage();
    Message.From = new MailAddress("[email protected]");
    Message.To.Add("[email protected]");
    Message.Body = "teste de email";
    Message.Subject = "Seja bem Vindo";
    cliente.Credentials = new System.Net.NetworkCredential("[email protected]", "UMA_SENHA");
    cliente.Port = 587;
    cliente.Send(Message);
}

If all SMTP server data is really correct (it is worth checking if the server requires SSL and add cliente.EnableSsl = true; if that’s the case), the only thing that could be blocking your message would be a firewall blocking port 587. Make sure that on your network port 587 is not blocked, it can be Windows Firewall, a router or even the modem.

Browser other questions tagged

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