title: Excel VBA CDO Mail
body: <p>I'm trying to send a mail with Microsoft Office Excel 2007 VBA code but I'm getting the error:</p>
  <blockquote>
  <p>Run-time error '-2147220973 (80040213)':</p>
  <p>Automation error</p>
  </blockquote>
  <p>The code that I'm using is:</p>
  <pre><code>Dim cdomsg As Object
  
  Set cdomsg = CreateObject(&quot;CDO.message&quot;)
  
  With cdomsg.Configuration.Fields
  
    .Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendusing&quot;) = 2
    .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smptserverport&quot;) = 25
    .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpserver&quot;) = &quot;smtp.gmail.com&quot;
    ' .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpauthenticate&quot;) = 1
    .Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendusername&quot;) = &quot;excel.**********@gmail.com&quot;
    .Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendpassword&quot;) = &quot;**********123&quot;
    ' .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpusessl&quot;) = True
    .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout&quot;) = 60
    .Update
  
  End With
  
  With cdomsg
  
    .Subject = &quot;Automated mail&quot;
    .From = &quot;excel.**********@gmail.com&quot;
    .To = &quot;**********@hitbts.com&quot; ' https://temp-mail.org/
    .TextBody = &quot;Automated mail&quot;
    .AddAttachment (&quot;*:\*****\***********\****************\***********\*****\*****.xlsm&quot;)
    .Send
  
  End With
  
  Set cdomsg = Nothing
  </code></pre>
  <p>I have tried other smpt servers, the server name and address that shows in the cmd when I type in <a href="https://stackoverflow.com/a/1461224">nslookup</a>, the computer's IP and another IP but I don't know what's the correct smpt server.</p>
  <p><strong>Edit after answer:</strong></p>
  <p>To anyone searching for this in the future, the code I used and worked is the following (taken from <a href="https://youtu.be/cOhupIT0rNA" rel="nofollow noreferrer">this</a> video):</p>
  <pre><code>Dim Mail As New Message
  Dim Config As Configuration
  Set Config = Mail.Configuration
  
  Config(cdoSendUsingMethod) = cdoSendUsingPort
  Config(cdoSMTPServer) = &quot;smtp.gmail.com&quot;
  Config(cdoSMTPServerPort) = 25
  Config(cdoSMTPAuthenticate) = cdoBasic
  Config(cdoSMTPUseSSL) = True
  Config(cdoSendUserName) = &quot;sender@gmail.com&quot;
  Config(cdoSendPassword) = &quot;password123&quot;
  Config.Fields.Update
  
  Mail.AddAttachment (&quot;C:\path\file.ext&quot;)
  Mail.To = &quot;destination@gmail.com&quot;
  Mail.From = Config(cdoSendUserName)
  Mail.Subject = &quot;Email Subject&quot;
  Mail.HTMLBody = &quot;&lt;b&gt;Email Body&lt;/b&gt;&quot;
  
  Mail.Send
  </code></pre>
  <p>Make sure to change <code>&quot;sender@gmail.com&quot;</code>, <code>&quot;password123&quot;</code>, <code>&quot;C:\path\file.ext&quot;</code> and <code>&quot;destination@gmail.com&quot;</code> for the example to work and the subject and body to change the mail.</p>
  <p>I also went to the top menu &quot;Tools&quot; on the VBA, option &quot;References...&quot;, enabled &quot;Microsoft CDO for Windows 2000 Library&quot; and pressed OK as shown in the video linked above.</p>
  <p><a href="https://www.google.com/settings/security/lesssecureapps" rel="nofollow noreferrer">Direct link</a> to enable the &quot;Less Secure&quot; option for GMail taken from <a href="https://www.rondebruin.nl/win/s1/cdo.htm" rel="nofollow noreferrer">here</a>.</p>