Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

outlook - How to use Microsoft Online Exchange with python to send mail

I want to send an Email from my outlook account using Python. I used the below code running in an AWS EC2 instance to generate an email.

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

server = smtplib.SMTP('smtp.office365.com', 587)
server.starttls()
server.login("<outlook_mail_id>","<outlook_password>")

server.sendmail("<outlook_mail_id>", "<recipient_email_id> ", "Test Message")
server.quit()

But while running this I get the below error

Traceback (most recent call last):
  File "smtp_office_365.py", line 12, in <module>
    server.login("<outlook_email_id>","<outlook_pass>")
  File "/usr/local/lib/python3.8/smtplib.py", line 734, in login
    raise last_exception
  File "/usr/local/lib/python3.8/smtplib.py", line 723, in login
    (code, resp) = self.auth(
  File "/usr/local/lib/python3.8/smtplib.py", line 646, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful [MN2PR15CA0001.namprd15.prod.outlook.com]')

I am able to login into web outlook account using these credentials. Hence to check further I connected with my Admin team and they said that Basic Authentication is disabled for this account and can't be enabled in any case. They suggested me to use modern Authentication method by using Microsoft Online Exchange.

On further exploration I found the below script to send the mail

from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, Configuration

credentials = Credentials('<outlook_mail_id>', '<outlook_password>')
account = Account('<outlook_mail_id>', credentials=credentials, autodiscover=True)
 
for item in account.inbox.all().order_by('-datetime_received')[:100]:
    print(item.subject, item.sender, item.datetime_received)

But I get the below error while running the script

Traceback (most recent call last):
  File "test_exchange.py", line 5, in <module>
    account = Account('<outlook_email_id>', credentials=credentials, autodiscover=True)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/exchangelib/account.py", line 116, in __init__
    self.ad_response, self.protocol = discover(
  File "/home/ec2-user/.local/lib/python3.8/site-packages/exchangelib/autodiscover/discovery.py", line 24, in discover
    return Autodiscovery(
  File "/home/ec2-user/.local/lib/python3.8/site-packages/exchangelib/autodiscover/discovery.py", line 123, in discover
    ad_response = self._step_1(hostname=domain)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/exchangelib/autodiscover/discovery.py", line 433, in _step_1
    return self._step_2(hostname=hostname)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/exchangelib/autodiscover/discovery.py", line 451, in _step_2
    return self._step_3(hostname=hostname)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/exchangelib/autodiscover/discovery.py", line 483, in _step_3
    return self._step_4(hostname=hostname)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/exchangelib/autodiscover/discovery.py", line 514, in _step_4
    return self._step_6()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/exchangelib/autodiscover/discovery.py", line 572, in _step_6
    raise AutoDiscoverFailed(
exchangelib.errors.AutoDiscoverFailed: All steps in the autodiscover protocol failed for email '<outlook_email_id>'. If you think this is an error, consider doing an official test at https://testconnectivity.microsoft.com

Please let me know if more details are required. Any help would be appreciated.

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...