Send Email Login and Password C # windows Form

2

I saw how to send email when the user forgets the password and put up a different code but all gave errors.

I think something was missing and could anyone help me?

follow 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 != "")
            {

// try 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);

// n3 attempt

                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);

            }

    
asked by anonymous 24.05.2015 / 16:37

2 answers

2

It would look 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 { }

Watch out for the information you should replace ... There may be some changes to the SMTP configuration portion depending on which server you choose to send.

    
25.05.2015 / 00:16
0

There's nothing wrong with your last code, you just have to Dispose of SmtpClient . You also do not need to convert the number 587 to integer because it is already an integer. The code I used, copied from yours, and it worked:

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 actually correct (it's worth checking if the server requires SSL and adding cliente.EnableSsl = true; if any), the only thing that could be blocking the sending of your message would be a firewall blocking the port 587. Make sure that on your network port 587 is not blocked, it may be Windows Firewall, a router or even the modem.

    
25.05.2015 / 01:00