Search The ForumSearch   RegisterRegister  LoginLogin

MailBee SMTP

 AfterLogic Forum : MailBee SMTP
Subject Topic: Send Email with Access Token (VBS) Post ReplyPost New Topic
Message << Prev Topic | Next Topic >>

Joined: 13 April 2022
Location: Germany
Online Status: Offline
Posts: 8
Posted: 09 August 2022 at 2:07am | IP Logged Quote romanovi4


i try to send a mail with access token from vbs-script. I use the client credentials flow to get the access token because the script will be run in background via scheduler. Fetching of access token works well. But send the mail doesn't work. Please review a sample code (based on mailbee example) - what is wrong?

----------------- Code ------------------------
MicrosoftSendEmailURL = "" & MicrosoftUserEmail & "/sendmail"

DataToSend = "client_id=" & MicrosoftClientId & "&client_secret=" & MicrosoftClientSecret & "&grant_type=client_credentials&"
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
XMLHTTP.Open "POST", "" & MicrosoftTenantId & "/oauth2/v2.0/token", False
XMLHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
XMLHTTP.Send DataToSend
JsonResponseString = XMLHTTP.ResponseText
MsgBox "JSON response:" & vbCrLf & JsonResponseString
Set JsonParser = New VbsJson
Set ParsedJson = JsonParser.Decode(JsonResponseString)
AccessToken = ParsedJson("access_token")
MsgBox "AccessToken: " & AccessToken

' This is just for debugging purposes, the code below never reads token.json
Set Msg = CreateObject("MailBee.Message")
Msg.WriteStringToFile "C:\Temp\token.json", JsonResponseString

' Send email to myself
' AuthMethod: 8 - OAuth, 2 - Normal
Set SMTP = CreateObject("MailBee.SMTP")
SMTP.EnableLogging = True
SMTP.LogFilePath = MailBeeSmtpLogFilePath
SMTP.LicenseKey = MailBeeLicenseKey
SMTP.ServerName = ""
SMTP.PortNumber = 587
SMTP.AuthMethod = 8
SMTP.SSL.UseStartTLS = True
SMTP.SSL.Protocol = 6
SMTP.UserName = MicrosoftUserEmail
SMTP.Password = AccessToken
SMTP.FromAddr = MicrosoftUserEmail
SMTP.Message.Subject = "Test email"
SMTP.Message.BodyText = "Test email"
If SMTP.Send Then
     MsgBox "Email sent"
     MsgBox "ErrCode=" & SMTP.ErrCode & ", " & SMTP.ErrDesc
End If

------------------ LOG ---------------------
10:40:54 [MailBee SMTP v9.4.0.263. Registered version]
10:40:54 [Connecting to server at port 587 using default domain]
10:40:54 [Domain not specified, using local host name]
10:40:54 [Entering ESMTP authentication mode]
10:40:54 [Sending EHLO]
10:40:54 [Switching to SSL over regular port mode]
10:40:54 [Initiating SSL Connection]
10:40:54 [Sending EHLO]
10:40:54 [Using XOAUTH2 ESMTP authentication]
10:40:54 [Sending encoded XOAUTH2 Request]
10:41:00 [Error: Negative or void server response]
10:41:00 [Server responded: 535 5.7.3 Authentication unsuccessful []\r\n]
10:41:00 [Possible error reason: incorrect UserName and/or OAuth2 access token]
10:41:00 [Error: Connection failure]

Best regards
Back to Top View romanovi4's Profile Search for other posts by romanovi4
AfterLogic Support
AfterLogic Support

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 09 August 2022 at 3:35am | IP Logged Quote Alex


We never had client credentials flow to work with Office 365. Last time we tried, Office 365 didn't support that. This has nothing to do with MailBee itself as MailBee does not generate tokens, it only consumes them.

I heard they changed this policy recently so there is a chance now credentials flow can be used somehow but how exactly its gonna work - it's subject to further research.

Back to Top View Alex's Profile Search for other posts by Alex

Joined: 13 April 2022
Location: Germany
Online Status: Offline
Posts: 8
Posted: 09 August 2022 at 4:33am | IP Logged Quote romanovi4

Yes, i took the example code with user credentials. And i try to addapt this example to my flow.
Back to Top View romanovi4's Profile Search for other posts by romanovi4

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide