【问题标题】:Sending email through Python通过 Python 发送电子邮件
【发布时间】:2016-04-19 04:46:47
【问题描述】:
#!/usr/bin/python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = "sender mail id"
toaddr = "receiver mail id"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test"
body = "Test mail"
msg.attach(MIMEText(body, 'plain'))
filename = "foo.txt"
attachment = open(r"F:\python\foo.txt", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

当我在本地系统上运行时,此脚本运行良好,但在专用网络上运行时却给了我一个错误。

OSError: [Win Error 10051] 尝试对无法访问的网络进行套接字操作。

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    您可能需要查看您是否可以从您的私人网络访问端口 587 上的 smtp.gmail.com

    专用网络管理员可能已设置网络以阻止端口 587 上的出站流量

    试试:

    telnet smtp.gmail.com 587
    

    如果您遇到无法访问的网络错误,则肯定是网络问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-14
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 2016-12-27
      • 2019-02-04
      相关资源
      最近更新 更多