Email with VBA without outlook

Shloime

New Member
Joined
Oct 25, 2023
Messages
48
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
I have the following code it does not work how can i get it work for using to send from a gmail

VBA Code:
'Build the text for the body of the message (could be read from ranges in Excel)
    mlText = "Thanks for buying from us" & vbNewLine &  _
                    "Please find you invoice attached."
   On Error GoTo Err:

   'early binding
   'Set NewMail = New CDO.Message
   'Set mailConfig = New CDO.Configuration
  
    Set NewMail = CreateObject("CDO.Message")
    Set mailConfig = NewMail.Configuration
   'load all default configurations
   mailConfig.Load -1
    EML = "**************@gmail.com"
    PAS = "Password"
   Set fields = mailConfig.fields
  
   'Set All Email Properties
   With NewMail
       .From = EML
       .To = "hellomyfriend@gmail.com"
       .cc = ""
       .BCC = ""
       .Subject = "Ithe subject"
       .TextBody = mlText ' & Str(Sheet1.Cells(2, 1))
       .Addattachment "c:\data\email.xlsx" 'Optional file attachment; remove if not needed.
       '.Addattachment "c:\data\email.pdf" 'Duplicate the line for a second attachment.
   End With

   msConfigURL = "http://schemas.microsoft.com/cdo/configuration"

   With fields
       .Item(msConfigURL & "/smtpusessl") = True 'Enable SSL Authentication
       .Item(msConfigURL & "/smtpauthenticate") = 1 'SMTP authentication Enabled
       .Item(msConfigURL & "/smtpserver") = "smtp.gmail.com" 'Set the SMTP server details
       .Item(msConfigURL & "/smtpserverport") = 465 'Set the SMTP port Details
       .Item(msConfigURL & "/sendusing") = 2 'Send using default setting
       .Item(msConfigURL & "/sendusername") = EML 'Your gmail address
       .Item(msConfigURL & "/sendpassword") = PAS '"fprmkutzyopfalft" 'Your password or App Password"
       .Update 'Update the configuration fields
   End With
   NewMail.Configuration = mailConfig
   NewMail.Send
 
   MsgBox "Your email has been sent", vbInformation

Exit_Err:
   'Release object memory
   Set NewMail = Nothing
   Set mailConfig = Nothing
   End

Err:
   Select Case Err.Number
   Case -2147220973 'Could be because of Internet Connection
       MsgBox "Check your internet connection." & vbNewLine & Err.Number & ": " & Err.Description
   Case -2147220975 'Incorrect credentials User ID or password
       MsgBox "Check your login credentials and try again." & vbNewLine & Err.Number & ": " & Err.Description
   Case Else 'Report other errors
       MsgBox "Error encountered while sending email." & vbNewLine & Err.Number & ": " & Err.Description
   End Select
 
Last edited by a moderator:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Please elaborate on "does not work". What happens?
 
Upvote 0
The following has worked for me in the past :

VBA Code:
Option Explicit

'For Early Binding, enable Tools > References > Microsoft CDO for Windows 2000 Library
Sub SendEmailUsingGmail()
    Dim NewMail As Object
    Dim mailConfig As Object
    Dim fields As Variant
    Dim msConfigURL As String
    On Error GoTo Err:

    'late binding
    Set NewMail = CreateObject("CDO.Message")
    Set mailConfig = CreateObject("CDO.Configuration")

    ' load all default configurations
    mailConfig.Load -1

    Set fields = mailConfig.fields

    'Set All Email Properties
    With NewMail
        .From = "youremail@gmail.com"
        .To = "recipient@domain.com"
        .CC = ""
        .BCC = ""
        .Subject = "Demo Spreadsheet Attached"
        .Textbody = "Let me know if you have questions about the attached spreadsheet!"
        '.Addattachment "c:\data\testmail.xlsx"
    End With

    msConfigURL = "http://schemas.microsoft.com/cdo/configuration"

    With fields
        .Item(msConfigURL & "/smtpusessl") = True             'Enable SSL Authentication
        .Item(msConfigURL & "/smtpauthenticate") = 1          'SMTP authentication Enabled
        .Item(msConfigURL & "/smtpserver") = "smtp.gmail.com" 'Set the SMTP server details
        .Item(msConfigURL & "/smtpserverport") = 465          'Set the SMTP port Details
        .Item(msConfigURL & "/sendusing") = 2                 'Send using default setting
        .Item(msConfigURL & "/sendusername") = "youremail@gmail.com" 'Your gmail address
        .Item(msConfigURL & "/sendpassword") = "yourpassword" 'Your password or App Password
        .Update                                               'Update the configuration fields
    End With
    NewMail.Configuration = mailConfig
    NewMail.Send
    
    MsgBox "Your email has been sent", vbInformation

Exit_Err:
    'Release object memory
    Set NewMail = Nothing
    Set mailConfig = Nothing
    End

Err:
    Select Case Err.Number
    Case -2147220973  'Could be because of Internet Connection
        MsgBox "Check your internet connection." & vbNewLine & Err.Number & ": " & Err.Description
    Case -2147220975  'Incorrect credentials User ID or password
        MsgBox "Check your login credentials and try again." & vbNewLine & Err.Number & ": " & Err.Description
    Case Else   'Report other errors
        MsgBox "Error encountered while sending email." & vbNewLine & Err.Number & ": " & Err.Description
    End Select

    Resume Exit_Err

End Sub
 
Upvote 0
The following has worked for me in the past :

VBA Code:
Option Explicit

'For Early Binding, enable Tools > References > Microsoft CDO for Windows 2000 Library
Sub SendEmailUsingGmail()
    Dim NewMail As Object
    Dim mailConfig As Object
    Dim fields As Variant
    Dim msConfigURL As String
    On Error GoTo Err:

    'late binding
    Set NewMail = CreateObject("CDO.Message")
    Set mailConfig = CreateObject("CDO.Configuration")

    ' load all default configurations
    mailConfig.Load -1

    Set fields = mailConfig.fields

    'Set All Email Properties
    With NewMail
        .From = "youremail@gmail.com"
        .To = "recipient@domain.com"
        .CC = ""
        .BCC = ""
        .Subject = "Demo Spreadsheet Attached"
        .Textbody = "Let me know if you have questions about the attached spreadsheet!"
        '.Addattachment "c:\data\testmail.xlsx"
    End With

    msConfigURL = "http://schemas.microsoft.com/cdo/configuration"

    With fields
        .Item(msConfigURL & "/smtpusessl") = True             'Enable SSL Authentication
        .Item(msConfigURL & "/smtpauthenticate") = 1          'SMTP authentication Enabled
        .Item(msConfigURL & "/smtpserver") = "smtp.gmail.com" 'Set the SMTP server details
        .Item(msConfigURL & "/smtpserverport") = 465          'Set the SMTP port Details
        .Item(msConfigURL & "/sendusing") = 2                 'Send using default setting
        .Item(msConfigURL & "/sendusername") = "youremail@gmail.com" 'Your gmail address
        .Item(msConfigURL & "/sendpassword") = "yourpassword" 'Your password or App Password
        .Update                                               'Update the configuration fields
    End With
    NewMail.Configuration = mailConfig
    NewMail.Send
   
    MsgBox "Your email has been sent", vbInformation

Exit_Err:
    'Release object memory
    Set NewMail = Nothing
    Set mailConfig = Nothing
    End

Err:
    Select Case Err.Number
    Case -2147220973  'Could be because of Internet Connection
        MsgBox "Check your internet connection." & vbNewLine & Err.Number & ": " & Err.Description
    Case -2147220975  'Incorrect credentials User ID or password
        MsgBox "Check your login credentials and try again." & vbNewLine & Err.Number & ": " & Err.Description
    Case Else   'Report other errors
        MsgBox "Error encountered while sending email." & vbNewLine & Err.Number & ": " & Err.Description
    End Select

    Resume Exit_Err

End Sub
Check your loin /credential and try again.
-2147220975: The message could not be sent to the smptp server. The transport code was 0x80040217. The server reponse was not available
 

Attachments

  • Screenshot (241).png
    Screenshot (241).png
    13 KB · Views: 21
Upvote 0
Check your loin /credential and try again.
-2147220975: The message could not be sent to the smptp server. The transport code was 0x80040217. The server reponse was not available
I tried the code with another email not gmail and it worked so it's probably some setting in gmail to change
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top