【问题标题】:Sending an Email from Python via Office 365 Corporate Account通过 Office 365 企业帐户从 Python 发送电子邮件
【发布时间】:2019-10-08 02:16:46
【问题描述】:

我正在尝试通过 Python 从我的 Office 365 企业帐户向另一个 Office 365 企业帐户发送电子邮件。目标是在脚本成功运行后发送电子邮件。

我检查了电子邮件 ID 和密码,但似乎无法找出问题所在。

    import smtplib
    message = "Execution Successful"
    mailserver = smtplib.SMTP('smtp.office365.com',587)
    mailserver.ehlo()
    mailserver.starttls()
    mailserver.login('userid@corporateemail.com', 'password')
    mailserver.sendmail('userid@corporateemail.com', 'userid@corporateemail.com', message)
    mailserver.quit()

这应该会触发给用户的电子邮件。但是,它会给出错误消息。 这是输出:

    Traceback (most recent call last):

  File "<ipython-input-45-663ff7ed4e61>", line 1, in <module>
    runfile('C:/Users/qy115/Desktop/Updated Python/Test/EmailTest.py', wdir='C:/Users/qy115/Desktop/Updated Python/Test')

  File "C:\Software\Eng_APPS\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Software\Eng_APPS\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/qy115/Desktop/Updated Python/Test/EmailTest.py", line 20, in <module>
    mailserver.starttls()

  File "C:\Software\Eng_APPS\Anaconda3\lib\smtplib.py", line 752, in starttls
    (resp, reply) = self.docmd("STARTTLS")

  File "C:\Software\Eng_APPS\Anaconda3\lib\smtplib.py", line 420, in docmd
    return self.getreply()

  File "C:\Software\Eng_APPS\Anaconda3\lib\smtplib.py", line 390, in getreply
    + str(e))

  SMTPServerDisconnected: Connection unexpectedly closed: [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

【问题讨论】:

  • 错误类型是什么?
  • 回溯似乎不完整。
  • @HenryYik 抱歉,我已编辑问题以添加缺少的错误消息。
  • 如果您无法解决问题,请查看 win32com.client。我完美地做到了你所描述的。
  • @MichaelButscher 我已经添加了缺失的部分。很抱歉之前错过了。

标签: python outlook office365 email


【解决方案1】:

我正在尝试做同样的事情。我认为您不能再使用用户名和密码对 Office 365 进行身份验证。您必须按照我的理解要求您通过 Microsoft Azure 服务中的安全应用程序连接到 Office 365 的说明进行操作:

https://pypi.org/project/O365/#different-authentication-interfaces

我已成功进行身份验证,但无法检索进入我的帐户和发送电子邮件所需的令牌。也许你会有更好的成功?如果有人成功完成此操作,请告诉我们,因为我无法解决。

【讨论】:

    【解决方案2】:
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    mail_content = "Hello, This is a simple mail. There is only text, no 
    attachments are there The mail is sent using Python SMTP library"
    #The mail addresses and password
    sender_address = 'userid@corporateemail.com'
    sender_pass = 'XXXXXXXXX'
    receiver_address = 'userid@corporateemail.com'
    #Setup the MIME
    message = MIMEMultipart()
    message['From'] = sender_address
    message['To'] = receiver_address
    message['Subject'] = 'A test mail sent by Python. It has an attachment.'   
    #The subject line
    #The body and the attachments for the mail
    message.attach(MIMEText(mail_content, 'plain'))
    #Create SMTP session for sending the mail
    session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
    session.starttls() #enable security
    session.login(sender_address, sender_pass) #login with mail_id and password
    text = message.as_string()
    session.sendmail(sender_address, receiver_address, text)
    session.quit()
    print('Mail Sent')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-22
      • 2017-10-22
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多