【发布时间】:2017-10-07 12:27:33
【问题描述】:
我在这里搜索了许多与 python 电子邮件相关的帖子,但我似乎无法使用 python 发送一封电子邮件。我在公司局域网内工作。但是如果我使用我公司的 smtp 中继服务器,我可以将电子邮件发送到 gmail,或者我的公司 ID 或其他电子邮件。这很奇怪,还是应该是这样?如何使用gmail smtp服务器向外发送邮件?
这是我尝试过的几个脚本。 =====================================1==
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("xyz@gmail.com", "myPasswd")
msg = "YOUR MESSAGE!"
server.sendmail("xyz@gmail.com", "xyz@gmail.com", msg)
server.quit()
====================================2==
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
msg = MIMEMultipart()
msg['From'] = 'sender@gmail.com'
msg['To'] = 'recvr@gmail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('sender@gmail.com', 'pswd')
mailserver.sendmail('sender@gmail.com','rcvr@gmail.com',msg.as_string())
mailserver.quit()
========================================
我确信这与代码无关,而是我缺少的其他东西。
【问题讨论】:
-
"无法发送" -> 你得到什么错误?你遇到了什么问题?
-
使用上面的python脚本什么都没有发生意味着没有错误消息。它只是停留,没有输出。所以几分钟后我检查是否发送了电子邮件 - 什么都没有。我必须手动停止运行时。
标签: python email server smtp send