在 python 中的 azure databricks notebook 中发送电子邮件(outlook 作为服务器)?

Sending email(outlook as server) in azure databricks notebook in python?

我尝试使用本地主机进行配置。我在我的 cmd 行中 运行 这个。 (python -m smtpd -c DebuggingServer -n localhost:1025)

import smtplib, ssl

smtp_server = "localhost"
port = 1025  # For starttls
sender_email = "my outlook email"
password =  input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    print("server connected")
    # TODO: Send email here
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit() 

但出现“连接被拒绝”错误。

要解决connection refused错误,您可以尝试以下方法:

  • 防火墙可能阻止了对端口 1025 的访问
  • 或者,您可以尝试 using port 25

根据documentation

For Enterprise Dev/Test subscriptions, port 25 is blocked by default. It is possible to have this block removed. To request to have the block removed, go to the Cannot send email (SMTP-Port 25) section of the Diagnose and Solve blade in the Azure Virtual Network resource in the Azure portal and run the diagnostic. This will exempt the qualified enterprise dev/test subscriptions automatically.

可以参考Send emails from Azure Databricks