title: How to send email via Gmail using Excel VBA? The message could not be sent to the SMPT server
body: <p>I found several variations to send email via Gmail in VBA and watched a YouTube demo using the sub below.<br />
  I tried smtpserverport 25 and 465.<br />
  I tried smtpserver smtp.gmail.com and smtp.office365.com.<br />
  I have the Microsoft CDO Reference selected.<br />
  I verified the Gmail password and get no error messages, no compile errors.</p>
  <p>I call the sub from the main routine above via <code>send_email_via_gmail</code>. I added a Msgbox just past the send statement to make sure the sub is called and it does reach it but I never get the emails. I have O365.</p>
  <pre class="lang-vb prettyprint-override"><code>Sub send_email_via_gmail()
  Dim myMail As CDO.Message
  Set myMail = New CDO.Message
  myMail.Configuration.fields.Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpusessl&quot;) = True
  myMail.Configuration.fields.Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpauthenticate&quot;) = 1
  myMail.Configuration.fields.Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpserver&quot;) = &quot;smtp.gmail.com&quot;
  myMail.Configuration.fields.Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpserverport&quot;) = 465
  myMail.Configuration.fields.Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendusing&quot;) = 2
  myMail.Configuration.fields.Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendusername&quot;) = &quot;xxxxxxx@gmail.com&quot;
  myMail.Configuration.fields.Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendpassword&quot;) = &quot;xxxxxxx&quot;
  myMail.Configuration.fields.Update
  With myMail
  .Subject = &quot;Test email from Stock App&quot;
  .From = &quot;xxxxxxx@gmail.com&quot;
  .To = &quot;xxxxxxx@comcast.net&quot;
  .TextBody = &quot;Test email from Me to Me&quot;
  End With
  On Error Resume Next
  myMail.Send
  Set myMail = Nothing
  End Sub
  </code></pre>
  <p>When I remove the <code>On Error</code> statement I do get a run-time error</p>
  <blockquote>
  <p>The message could not be sent to the SMPT server. The transport error code was 0x80040217. The server response was not available.</p>
  </blockquote>