title: How to send an e-mail with C# through Gmail
body: <p>I am getting an error when trying to send an e-mail through my web service. I have tried enabling access to less secure apps disabling 2-step verification and logging into the account via a web browser. None of the solutions on SO have worked for me. I am still getting:</p>
  
  <blockquote>
    <p>Error: System.Net.Mail.SmtpException: The SMTP server requires a
    secure connection or the client was not authenticated. The server
    response was: 5.5.1 Authentication Required.</p>
  </blockquote>
  
  <p>What can I do to fix this issue?</p>
  
  <pre><code>namespace EmailService
  {
      public class Service1 : IService1
      {    
          public string SendEmail(string inputEmail, string subject, string body)
          {
              string returnString = "";
              try
              {
                  MailMessage email = new MailMessage();
                  SmtpClient smtp = new SmtpClient();
                  smtp.Host = "smtp.gmail.com";
  
                  // set up the Gmail server
                  smtp.EnableSsl = true;
                  smtp.Port = 587;
                  smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
                  smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                  smtp.UseDefaultCredentials = false;
  
                  // draft the email
                  MailAddress fromAddress = new MailAddress("cse445emailservice@gmail.com");
                  email.From = fromAddress;
                  email.To.Add(inputEmail);
                  email.Subject = body;
                  email.Body = body;
  
                  smtp.Send(email);
  
                  returnString = "Success! Please check your e-mail.";
              }
              catch(Exception ex)
              {
                  returnString = "Error: " + ex.ToString();
              }
              return returnString;
          }
      }
  }
  </code></pre>