在python中发送邮件时出错

时间:2016-02-12 11:55:28

标签: python email smtplib

pwd = "mypassword"
import smtplib

server = smtplib.SMTP('smtp.gmail.com')
server.ehlo()
server.starttls()
server.login("gvpcse113@gmail.com",pwd)

msg = "YOUR MESSAGE!"
server.sendmail("gvpcse113@gmail.com", "sender@xyz.com", msg)
server.quit()

我试过通过python发送邮件...
错误:

Traceback (most recent call last):
  File "H:/my projects/PythonCourse/test_cont/mail_test4.py", line 4, in <module>
    server = smtplib.SMTP('smtp.gmail.com')
  File "C:\Python27\lib\smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python27\lib\smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python27\lib\smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "C:\Python27\lib\socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed

我通过代理连接进行连接
我通过Windows中的cmd设置代理。 请帮帮我。

更新:
我确信通过互联网连接:

import urllib2

def internet_on():
    try:
        response=urllib2.urlopen('https://www.google.co.in',timeout=1)
        return True
    except urllib2.URLError as err: pass
    return False

print internet_on()

输出为True

2 个答案:

答案 0 :(得分:5)

您的代码对我来说很好,所以它可能是连接设置。

尝试将服务器更改为:

server = smtplib.SMTP('64.233.184.108')

(这是smtp.gmail.com的IP地址,绕过DNS解析)

答案 1 :(得分:0)

尝试更新

server = smtplib.SMTP('smtp.gmail.com')
以包含端口。 gmail的端口是587,因此请将其添加为第二个参数:
server = smtplib.SMTP('smtp.gmail.com', 587)

如果这不起作用,请确保您已使用此网站以确保该帐户允许您进入:

https://www.google.com/settings/security/lesssecureapps