2

I have been using the following code to send emails from excel to a nominated person. It works fine if I go into my gmail and turn on "Use Less Secure Devices". Google intend to discontinue this option. How can I modify my code to overcome this hurdle. Any advice would be extremely welcome.

Sub MyGmailMacro()
Dim iMsg As Object
Dim iConf As Object
Dim Flds As Variant

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1
Set Flds = iConf.Fields

With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myemail"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com" 'smtp mail server
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 'stmp server
    .Update
End With

With iMsg
    Set .Configuration = iConf
    .To = MyReceiver
    .From = "myemail"
    .Subject = MySubject
    .TextBody = MyMessage
    

    .AddAttachment "C:\Attach1.doc"
    .AddAttachment "C:\Attach2.doc"
    
    .Send
   
End With

Set iMsg = Nothing
Set iConf = Nothing

End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
willi
  • 33
  • 7
  • 1
    While google going to turn off `Less Secure App` then google already implemented app passwords. So, you can use [App Password](https://support.google.com/accounts/answer/185833?hl=en). – Harun24hr Apr 19 '22 at 02:54
  • App password requires two-step verification. it is not feasible in this case as the excel spreadsheet is used by various users. Willi – willi Apr 21 '22 at 03:11
  • 1
    Two step verification to logon gmail but excel vba will not require two step verification. – Harun24hr Apr 21 '22 at 06:15
  • 1
    I confirm that @Harun24hr answer works, thanks a lot ! – Ewdlam Aug 25 '22 at 12:01

0 Answers0