I am trying to read and send emails (O365) using OAuth2. This code works fine on my local machine, but
when I am trying to deploy this in a server where there is a proxy to access the internet, I am getting the below error.
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='login.microsoftonline.com', port=443): Max retries exceeded with url: /2d11ad74-bbf6-403f-32c7-6ff0e039a923/oauth2/v2.0/token (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002507067C348>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
Below is my code I tried:
import settings
from exchangelib import Credentials, Account, DELEGATE, BASIC, \
Configuration, NTLM, Message, Mailbox, FileAttachment, HTMLBody, FaultTolerance, IMPERSONATION
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
from exchangelib import OAuth2LegacyCredentials
credentials = OAuth2LegacyCredentials(
client_id= settings.clientId, client_secret= settings.clientSecret,
tenant_id=settings.tenentId,
username=settings.eUserName, password=settings.ePassword
)
config = Configuration(server = 'smtp.office365.com', credentials=credentials)
account = Account(settings.eUserName, config=config, access_type=DELEGATE)
m = Message(
account=account,
subject='Any Subject OAth2 Authentication',
body=HTMLBody('OAth2 Authentication'),
to_recipients=map(lambda x: Mailbox(email_address=x), ['myEmail@gmail.com'])
)
for item in account.inbox.all()[:10]:
print (item.sender.email_address)
m.send()
print('Email is being sent...')