MS Access Send Email VBA Using OAuth2 Office 365

623 Views Asked by At

I have an MS Access VBA application that sends automatic emails using an Office 365 email account. It has been working perfectly fine, but it will stop working soon when Microsoft stops using Basic Authentication, I already registered the app in Microsoft Azure and did all the steps there to get it working, but I don't know the code in VBA to update my current function inside of the MS Access Application.

Here is my current code using Basic Authentication:

Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
 
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
 
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/sendtls") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "outlook.office365.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Update
End With
      
With iMsg
    Set .Configuration = iConf
    .to = "[email protected]"
    .CC = ""
    .BCC = ""
    .FROM = "[email protected]"
    .subject = "subject"
    .TextBody = "body"
    
    .Send
End With    
1

There are 1 best solutions below

0
Eugene Astafiev On

CDO is not supported and not recommended for using any longer. Instead, you may consider automating Outlook where you don't need to care about authentication mechanisms. Here is what Microsoft states for that:

Microsoft Outlook 2010 and later versions include many architectural changes to the client-side MAPI subsystem. Of particular concern are scenarios in which Outlook is configured to use multiple Exchange accounts. Also, CDO 1.2.1 is a 32-bit client library and will not operate with 64-bit versions of Outlook. Given all these factors, CDO 1.2.1 is not supported for use with Outlook 2010 or later versions, and we do not recommend its use with Outlook 2010 and later versions.

Programs that use CDO should be redesigned to use other Application Programming Interfaces (APIs) instead of CDO. Starting with Outlook 2007, the Outlook object model was greatly expanded to provide functionality that was previously available only by using CDO 1.2.1. The Outlook 2010 and later versions object model includes some new features to expand on this more. For example, the Outlook object model has new functionality to operate correctly with multiple Exchange accounts. The Outlook object model also works for both 32-bit and 64-bit versions of Outlook. Developers should use the Outlook 2010 and later object model instead of CDO 1.2.1.