title: Send Email via C# through Google Apps account
body: <p>I have a standard Google Apps account. I have setup a custom domain through Google Apps. I am able to send and receive emails successfully through Google Apps when I use the Gmail interface. However, I want to send an email via code. In order to attempt this, I have been trying the following code:</p>
  <pre><code>MailMessage mailMessage = new MailMessage();
  mailMessage.To.Add(&quot;someone@somewhere.example&quot;);
  mailMessage.Subject = &quot;Test&quot;;
  mailMessage.Body = &quot;&lt;html&gt;&lt;body&gt;This is a test&lt;/body&gt;&lt;/html&gt;&quot;;
  mailMessage.IsBodyHtml = true;
  
  // Create the credentials to login to the gmail account associated with my custom domain
  string sendEmailsFrom = &quot;emailAddress@mydomain.example&quot;;
  string sendEmailsFromPassword = &quot;password&quot;;
  NetworkCredential cred = new NetworkCredential(sendEmailsFrom, sendEmailsFromPassword);
  
  SmtpClient mailClient = new SmtpClient(&quot;smtp.gmail.com&quot;, 587);
  mailClient.EnableSsl = true;
  mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
  mailClient.UseDefaultCredentials = false;
  mailClient.Timeout = 20000;
  mailClient.Credentials = cred;
  mailClient.Send(mailMessage);
  </code></pre>
  <p>When the Send method is reached, an Exception is thrown that states:</p>
  <blockquote>
  <p>&quot;The SMTP server requires a secure
  connection or the client was not
  authenticated. The server response
  was: 5.5.1 Authentication Required.&quot;</p>
  </blockquote>
  <p>How do I send emails through my custom domain via Google?</p>