【问题标题】:Email through smtplib通过 smtplib 发送电子邮件
【发布时间】:2013-08-04 11:25:50
【问题描述】:

我正在尝试构建一个可以通过 Gmail 发送电子邮件的 python 脚本。

当我尝试连接到 Gmail 服务器时,我收到错误 Errno 10013

这就是我想要做的:

gmail_message = smtplib.SMTP('smtp.gmail.com')
gmail_message.starttls()
gmail_message.login('xyz@gmail.com','xyzpassword')
gmail_message.sendmail('xyz@gmail.com',['xyz@gmail.com'], msg.as_string())
gmail_message.quit()

错误:

error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

我能做些什么来解决这个问题?

【问题讨论】:

  • 你试过here给出的解决方案了吗?

标签: python smtplib


【解决方案1】:

您可能需要指定端口

gmail_message = smtplib.SMTP(host='smtp.gmail.com', port=587)

【讨论】:

  • 我得到了同样的错误,我将端口号更改为 25 仍然是同样的事情。
  • 您使用的是什么操作系统?也许你没有被授权打开一个套接字来发送电子邮件。
  • 你有管理员权限吗?
  • 所以如果我用 Ubuntu 来做这个,会容易很多吗?
【解决方案2】:

这是在 Win7 上为我工作的代码。和你的差不多。

server = smtplib.SMTP('smtp.gmail.com:587')  
server.starttls()  
server.login(username,password)  
for comp in comps:
    msg = "Subject:CommenceSweep:Comp%s-%s\n\n" % (comp,sweep)
    server.sendmail(fromaddr, toaddrs, msg)  
server.quit()  

注意:您是否正确配置 Gmail 以允许 SMTP?

【讨论】:

  • 我还没有配置 Gmail,但我在 Ubuntu 中尝试了上面的代码,它运行良好。变量compssweep 是什么?
  • 它们只是字符。抱歉,我将它用于我的一个项目。它们只是普通的字符串。 msg 只是一个字符串。不过,我很确定您需要配置 Gmail……除非您已经这样做了。因为没有它,gmail 会拒绝我的连接。
猜你喜欢
  • 2021-04-26
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 2020-09-19
  • 2023-03-29
  • 2018-12-09
  • 2015-09-07
  • 2016-11-04
相关资源
最近更新 更多