从Azure databricks python notebook发送电子邮件时出现“gaireror:[Errno -5]没有与主机名关联的地址”

uqzxnwby  于 2023-01-19  发布在  Python
关注(0)|答案(2)|浏览(103)

我正在尝试通过公司SMTP服务器从Azure数据库笔记本发送邮件。我可以在本地计算机上运行相同的脚本。并且从本地运行脚本时正在发送邮件。
但是,当我从数据块运行脚本时,我收到错误 No address associated with hostname
剧本-

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

server = smtplib.SMTP("COMPANY SMTP SERVER", COMPANY_SMTP_PORT)
fromaddr = "username@companyname.com"
toaddr = "username@companyname.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Email test"
body = "test mail"
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)

错误追溯-

gaierror                                  Traceback (most recent call last)
<command-2889814627634467> in <module>
      3 from email.mime.text import MIMEText
      4 
----> 5 server = smtplib.SMTP("COMPANY SMTP SERVER", COMPANY_SMTP_PORT)
      6 fromaddr = "username@companyname.com"
      7 toaddr = "username@companyname.com"

/usr/lib/python3.8/smtplib.py in __init__(self, host, port, local_hostname, timeout, source_address)
    253 
    254         if host:
--> 255             (code, msg) = self.connect(host, port)
    256             if code != 220:
    257                 self.close()

/usr/lib/python3.8/smtplib.py in connect(self, host, port, source_address)
    337             port = self.default_port
    338         sys.audit("smtplib.connect", self, host, port)
--> 339         self.sock = self._get_socket(host, port, self.timeout)
    340         self.file = None

有谁能提供一些见解,可能是什么问题,我如何纠正它?

yjghlzjz

yjghlzjz1#

COMPANY SMTP SERVER字符串替换为电子邮件服务器的实际主机名。

xwbd5t1u

xwbd5t1u2#

能够通过使用gmail smtp服务器发送邮件来解决。
Databricks是我公司outlook邮件服务器的外部环境,所以通过公司smtp服务器发送邮件需要白名单。

相关问题