I have a corporative Microsoft Exchange account and a Python application. I would like to send emails from this account from my Python application with a token authentication, not with the simple (email_address, password)
.
I already have my Python application registered on my Microsoft Azure portal, have my client ID, token ID and everything. Could already obtain the token from the following lines:
import msal
app = msal.ConfidentialClientApplication(
client_id=param_client_id,
client_credential=param_client_credential,
authority=f"https://login.microsoftonline.com/{param_tenant_name}"
)
result = app.acquire_token_for_client(scopes=scopes)
mytoken = result['access_token']
I tried to follow the steps on https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#authenticate-connection-requests to edit the User_ID I was sending to the SMTP connection, but was not really successful.
The question is now what the best way is to gain access to my Exchange account via the token and send emails. I guess I cannot use the library smtplib
as it does not support authentication (only the simple credentials I mentioned above I'd like to avoid) and keeps rejecting my connection requests.
Note I have already ticked the box "Authenticated SMTP" on Microsoft 365 admin center --> Users --> Active users --> (Myself) --> Manage email apps and there should not be any Conditional Access Policy preventing myself from connecting. So it's not a matter a permissions but using a better Python library, I would say. Or maybe there is some way of changing the (email_address, password)
via the token to gain authenticated access??
Thanks in advance! Any help would be much appreciated!