Search The ForumSearch   RegisterRegister  LoginLogin

MailBee SMTP

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


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

Hello,

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 = "https://outlook.office.com/api/v2.0/users/" & MicrosoftUserEmail & "/sendmail"

DataToSend = "client_id=" & MicrosoftClientId & "&client_secret=" & MicrosoftClientSecret & "&grant_type=client_credentials&scope=https%3A%2F%2Foutlook.office365.com%2F.default"
Set XMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
XMLHTTP.Open "POST", "https://login.microsoftonline.com/" & 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.ClearLog
SMTP.LicenseKey = MailBeeLicenseKey
SMTP.ServerName = "smtp.office365.com"
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"
Else
     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 smtp.office365.com 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 [AM5PR0601CA0057.eurprd06.prod.outlook.com]\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
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

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

Hello,

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.

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 
romanovi4
Newbie
Newbie


Joined: 13 April 2022
Location: Germany
Online Status: Offline
Posts: 7
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