0

I created a form which includes a Send button and a richtextbox and Username and Passsword and From and To textboxes.

This is the code in the send button but unfortunately the program will close when I fill in the textboxes and click on the send button.

 private void btnSend_Click(object sender, EventArgs e)
 {
     MailMessage mail = new MailMessage(from.Text, to.Text, Subject.Text, body.Text);
      // SmtoServer = smtp.company.com; Ex: Gmail . smtp.gmail.com | yahoo - smtp.yahoo.com
      SmtpClient client = new SmtpClient(smtp.Text);
      client.Port = 587;
      client.Credentials = new System.Net.NetworkCredential(username.Text,password.Text);
      client.EnableSsl = true;
      client.Send(mail);
      MessageBox.Show("Your mail has been sent!\n Thanks for your email",
                      "Success", MessageBoxButtons.OK);

 }

Update I tried whatever you said and i already watch the Youtube video but compiler show me error in the line 9 :

client.Send(mail);

and this is the error :

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

  • 1
    You should include the rest of the code related to this issue. – Alexander Bell Mar 26 '16 at 16:41
  • The first thing to do is to add error checking; [see here for error handling the Send call](https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage%28v=vs.110%29.aspx) ! If it still closes step through your code with the debugger to see which line is the problem.. - I tried your code with my data and it works just fine. Note that you may want to reduce the timeout of the cient to a few seconds, so you don't have to wait so long.. – TaW Mar 26 '16 at 19:04
  • 1) you really should add the __error handling__ so the program behaves properly instead of throwing the exception. You will need that in any case! 2) Do look into __all__ of [this post](http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not), __not__ just the accepted answer! – TaW Mar 27 '16 at 07:05
  • @TaW Thanks Bro. It was my problem that my 2 verification steps was enable in my gmail account and i turned off and resolve my problem. Thanks for your link and for your help. – VorTex.Zerg Mar 27 '16 at 14:34

0 Answers0