title: Unable to send email using smtp in c#
body: <p>I'd like my system to send email when a button is clicked, I however keep getting a problem at smtp.send(message) everytime, there is no error or anything, it just kind of freezes although its still running. I have written 4 attempts and all are unsuccessful.</p>
  
  <p><strong>1st attempt:</strong></p>
  
  <pre><code>try
  {
      MailMessage mail = new MailMessage();
      SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
  
      mail.From = new MailAddress("robinx0852@gmail.com");
      mail.To.Add("robinx0852@gmail.com");
      mail.Subject = "Test Mail";
      mail.Body = "This is for testing SMTP mail from GMAIL";
  
      SmtpServer.Port = 25;
      SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
      SmtpServer.EnableSsl = true;
  
      SmtpServer.Send(mail);
      MessageBox.Show("mail Send");
      MessageBox.Show("Email sent successfully");
  }
  catch (Exception ex)
  {
      MessageBox.Show(ex.Message);
  }
  </code></pre>
  
  <p><strong>2nd attempt:</strong></p>
  
  <pre><code>try
  {
      //string s = $"User {edtID} has failed to login";
  
      MailMessage message = new MailMessage();
      message.To.Add("robinx0852@gmail.com");
      message.Subject = "Employee login failure";
      message.From = new MailAddress("armeepatel@gmail.com");
      message.Body = "Hello";
      SmtpClient smtp = new SmtpClient("smtp.saix.net");
      smtp.Send(message);
      MessageBox.Show("mail Sent");
  }
  catch (Exception ex)
  {
      MessageBox.Show(ex.Message);
  }
  
  SmtpClient smtp = new SmtpClient();
  smtp.Port = 25;
  smtp.Credentials = new System.Net.NetworkCredential("username", "password");
  smtp.EnableSsl = true;
  MailMessage mailMessage = new MailMessage();
  mailMessage.To.Add(new MailAddress("mymail@gmail.com", "ME"));
  mailMessage.Subject = "Some Subject";
  mailMessage.Body = "Test";
  smtp.Send(mailMessage);
  </code></pre>
  
  <p><strong>3rd attempt:</strong></p>
  
  <pre><code>// string s = $"User {edtID} has failed to login";
  MailMessage mail = new MailMessage();
  SmtpClient SmtpServer = new SmtpClient();
  mail.To.Add("mymail@gmail.com");
  mail.From = new MailAddress("hotelfrontdesk@testhotel.com");
  mail.Subject = "Employee login failure";
  mail.IsBodyHtml = true;
  mail.Body = "Hello";
  SmtpServer.Host = "smtp.saix.net";
  SmtpServer.Port = 25;
  SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
  try
  {
  
      SmtpServer.Send(mail);
  }
  catch (Exception ex)
  {
      MessageBox.Show("Exception Message: " + ex.Message);
      if (ex.InnerException != null)
          MessageBox.Show("Exception Inner: " + ex.InnerException);
  
  }
  </code></pre>
  
  <p><strong>4th attempt:</strong></p>
  
  <pre><code>var sysLogin = "yourlogin@gmail.com";
  var sysPass = "y0urP@ss";
  var sysAddress = new MailAddress(sysLogin, "Message from me!");
  
  var receiverAddress = new MailAddress("mymail#gmail.com");
  
  var smtp = new SmtpClient
  {
      Host = "smtp.gmail.com",   //gmail example
      Port = 587,
      EnableSsl = false,
      DeliveryMethod = SmtpDeliveryMethod.Network,
      UseDefaultCredentials = false,
      Credentials = new System.Net.NetworkCredential(sysLogin, sysPass)
  };
  
  
  using (var message = new MailMessage(sysAddress, receiverAddress) { Subject = "Some subject", Body = "Some text" })
  {
      smtp.Send(message);
  }
  MessageBox.Show("Success");
  </code></pre>