我正在不具有Internet连接的开发服务器上使用python脚本发送邮件,但我通过对服务器进行ping检查来检查与SMTP服务器的连接。但是使用smtplib连接到它时出现错误(11004,getaddrinfo失败)。
请帮助我解决这个问题! 下面是我使用的代码
msg = MIMEMultipart()
msg["From"] = "sender"
msg["To"] = 'recipient'
msg["Subject"] = "Testing Email"
message = 'Hi,\n \nThis is testing mail.'
ctype, encoding = mimetypes.guess_type(file)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
fp = open(file, "rb")
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename='report.xlsx')
msg.attach(MIMEText(message, 'plain'))
msg.attach(attachment)
try:
server = smtplib.SMTP(server_name)
server.set_debuglevel(0)
server.sendmail(sender, receiver, msg.as_string())
server.quit()
except Exception as e:
print(e.args)
答案 0 :(得分:0)
尝试一下:
server = smtplib.SMTP("smtp.google.com", 587)
server.starttls()
server.login(email, password)
server.sendmail(email, email, message)
server.quit()