E-mail to my account c#

Asked

Viewed 478 times

-1

When creating my program, I put a Form so that you can "Report Error/Suggestions". In this way I put 2 textbox and 1 button. 1 textbox for Subject, and the other so that you can write what you want (message). The button serves to send this data to my email. The code I have is as follows::

    if (!(txtpara.Text.Trim() == ""))
        {
            To = txtpara.Text;
            Subject = txtassunto.Text;
            Body = txtmensagem.Text;

            mail = new MailMessage();
            mail.To.Add(new MailAddress(this.To));
            mail.From = new MailAddress("[email protected]");
            mail.Subject = Subject;
            mail.Body = Body;
            mail.IsBodyHtml = false;

            SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
            using (client)
            {
                client.Credentials = new System.Net.NetworkCredential("[email protected]", "aminhapassword");
                client.EnableSsl = true;
                client.Send(mail);
            }
            MessageBox.Show("E-Mail enviado com Sucesso!", "Sucesso");
        }

The mistake is in:

"client. Send(mail);", and says: "Failed to send email" - Smtpexception.

inserir a descrição da imagem aqui

  • 1

    Welcome to the en OS. Please read How to ask a good question?. You have not reported what you have done so far, what are your specific doubts or problems you are having.

  • Edited friend @Molx.

  • 1

    @Godfathersantanta, you described the problem better, but I will insist a little. You need to be clearer. If you have no idea where to start, maybe the ideal is to look for tutorials like How to send email with C code#?. In the OR, we seek to answer more specific questions. For example, if you’re a code ready but it doesn’t work, you could post the important part for someone to take a look at.

  • Dear @Molx, that’s what I’ve done, but at the time of "client.Send(message)", it always gives me error. I’ve tried several methods without success...

  • Could you put more information on your smtp server? Sometimes the problem is just that it is not connected or something like that... Still, if you can detail better the SMTP exception that he is releasing, it already helps as well.

  • Hi @Felipeavelar, how so more information about my smtp server? I have to do something in my gmail account?

Show 1 more comment

1 answer

3

Probably the door of SMTP that you are using is incorrect, I know it is possible to use the 25, the 465 and the 587, but I was only successful using the 587.

System.Net.Mail.SmtpClient Smtp = new SmtpClient();
Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
Smtp.UseDefaultCredentials = false;
Smtp.Timeout = 300000;
Smtp.Host = "smtp.gmail.com";
Smtp.Port = 587;
Smtp.EnableSsl = true;
Smtp.Credentials = new System.Net.NetworkCredential("xxx", "xxx");
Smtp.Send(Mensagem);

This is a code running in production for sending email, using a gmail account.

@Edit for comments: To capture the Exception text, add this line instead of the last:

try{
    Smtp.Send(Mensagem);
} catch( Exception ex) { 
    string s = ex.ToString(); //Tratar a exceção
}

Remembering that this Try/catch implementation is only for receiving data from your exception, it is not recommended to treat exceptions in this way.

  • Hi @Marciano.Andrade, give me error in "Smtp.Send(Message);".

  • @Godfathersantana Have as you paste all the text of Exception here? Not only the result of .Message, but rather of the .ToString() of Exception.

  • What do you mean, friend? There’s no text... @Marciano.Andrade

  • @Godfathersantana takes a look at the edition of my answer, there I show you how to get the value I want. I would like you to paste here the value that the string s receive

  • The problem remained friend @Marciano.Andrade...

  • Suggestion, a quick way to capture exceptions without having to interrupt program execution Debug.WriteLine(ex.Message). The message appears in the Output of VS. (Also useful to print the stack trace of exception)

  • then @Godfathersantana, need you to capture all the stack trace of the exception, for this you need to put a breakpoint within the catch that I changed in my reply and pass me the value that will be within the string s, or else use the Omni method, but without the stack trace it will be difficult to help you, until your network may be blocking that code from working.

  • @Godfathersantana, you can also serialize the exception as JSON and paste it here, follow an example of how to serialize an Exception using Nuget Newtonsoft Json: https://dotnetfiddle.net/wAKnL4

  • @Tobymosque, I pasted the code there, but I didn’t notice anything... It’s just mistakes!

Show 4 more comments

Browser other questions tagged

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