简体   繁体   English

使用gmail通过gmail发送电子邮件

[英]Sending email through gmail with Python

So I have tried at least 5-10 examples online to send an email, but I keep getting the same error. 因此,我已经尝试了至少5-10个在线示例来发送电子邮件,但是我仍然遇到相同的错误。

Traceback (most recent call last):
  File "C:/Users/alxu/Project/LogFilter.py", line 88, in ?
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
  File "C:\Python24\lib\smtplib.py", line 244, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python24\lib\smtplib.py", line 292, in connect
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
socket.gaierror: (11004, 'getaddrinfo failed')

The last example I used code was like this... 我使用代码的最后一个示例是这样的...

import smtplib

to = 'mkyong2002@yahoo.com'
gmail_user = 'mkyong2002@gmail.com'
gmail_pwd = 'yourpassword'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + '\n this is test msg from mkyong.com \n\n'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()

I am using Python 2.4.3. 我正在使用Python 2.4.3。

EDIT: McAfee is has host intrusion prevention so all the times I attempted to do this it blocked it. 编辑:迈克菲(McAfee)具有主机入侵防护功能,因此我尝试执行此操作的所有时间都将其阻止。

Your last example code should work. 您的最后一个示例代码应该可以工作。 One thing you need to do is to make sure you allow access to less secure apps by going to the following link. 您需要做的一件事是通过转到以下链接来确保允许访问不太安全的应用程序。

google settings for secure apps 用于安全应用程序的Google设置

The reason for this is google flags such automated email scripts as less secure. 原因是Google将此类自动电子邮件脚本标记为安全性较低。 You should also be aware of the risks. 您还应该意识到这些风险。 Make sure you change this setting back if you are not gonna need it. 如果您不需要此设置,请确保将其更改回去。

Since you are behind a corporate proxy, you can't directly connect to any outside server, you need to connect through the proxy. 由于您位于公司代理的后面,因此您无法直接连接到任何外部服务器,因此需要通过代理进行连接。 In this case, you're trying to connect to a SMTP server, which uses port 25 or 587. I assume you're behind a HTTP proxy that doesn't block SMTP ports. 在这种情况下,您尝试连接到使用端口25或587的SMTP服务器。我假设您位于不阻止SMTP端口的HTTP代理后面。 If your corporate proxy blocks these ports, then there's nothing you can do to connect to the server. 如果您的公司代理阻止了这些端口,那么您将无法执行任何连接服务器的操作。

The smtplib module doesn't include the functionality to connect to a SMTP server through a proxy. smtplib模块不包含通过代理连接到SMTP服务器的功能。 As a workaround, I wrote the custom class ProxySMTP , which I have posted here along with instructions on how to use it. 作为一种解决方法,我编写了自定义类ProxySMTP ,并将其与如何使用的说明一起发布在此处

Let me know if it works for you. 请让我知道这对你有没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM