如何使用Python 3.4使用GMail发送电子邮件?

时间:2015-12-16 09:00:17

标签: python email

我一直在尝试一些代码,例如:

import smtplib

def sendemail(from_addr, to_addr_list, cc_addr_list,
              subject, message,
              login, password,
              smtpserver='smtp.gmail.com:587'):
    header  = 'From: %s\n' % from_addr
    header += 'To: %s\n' % ','.join(to_addr_list)
    header += 'Cc: %s\n' % ','.join(cc_addr_list)
    header += 'Subject: %s\n\n' % subject
    message = header + message

    server = smtplib.SMTP(smtpserver)
    server.starttls()
    server.login(login,password)
    problems = server.sendmail(from_addr, to_addr_list, message)
    server.quit()

sendemail(from_addr    = 'python@RC.net', 
        to_addr_list = ['example@gmail.com'],
        cc_addr_list = ['example@gmail.com'], 
        subject      = 'Howdy', 
        message      = 'Hello!', 
        login        = 'example@gmail.com', 
        password     = 'XXXX')

除了实际密码和详细信息外。

它带来了这个错误:

Traceback (most recent call last):
  File "User/Python/Email.py", line 27, in <module>
    password     = 'lollol69')
  File "User/Python/Email.py", line 15, in sendemail
    server = smtplib.SMTP(smtpserver)
  File "C:\Python34\lib\smtplib.py", line 242, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python34\lib\smtplib.py", line 321, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python34\lib\smtplib.py", line 292, in _get_socket
    self.source_address)
  File "C:\Python34\lib\socket.py", line 509, in create_connection
    raise err
  File "C:\Python34\lib\socket.py", line 500, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

理想情况下,我可以知道代码有什么问题以及如何解决这个问题。如果它完全错误,那么使用Gmail的一些工作代码将非常受欢迎。

谢谢!

1 个答案:

答案 0 :(得分:1)

您必须拆分smpt服务器地址和端口,并将它们用作smtplib.SMTP('smtp.gmail.com', 587)的单独参数。请参阅https://docs.python.org/3.4/library/smtplib.html


import smtplib

def sendemail(from_addr, to_addr_list, cc_addr_list,
              subject, message,
              login, password,
              smtpserver='smtp.gmail.com', smtpport=587):  # split smtpserver and -port
    header  = 'From: %s\n' % from_addr
    header += 'To: %s\n' % ','.join(to_addr_list)
    header += 'Cc: %s\n' % ','.join(cc_addr_list)
    header += 'Subject: %s\n\n' % subject
    message = header + message

    server = smtplib.SMTP(smtpserver, smtpport)  # use both smtpserver  and -port 
    server.starttls()
    server.login(login,password)
    problems = server.sendmail(from_addr, to_addr_list, message)
    server.quit()

sendemail(from_addr    = 'python@RC.net', 
        to_addr_list = ['example@gmail.com'],
        cc_addr_list = ['example@gmail.com'], 
        subject      = 'Howdy', 
        message      = 'Hello!', 
        login        = 'example@gmail.com', 
        password     = 'XXXX')

顺便说一下:更改你在堆栈跟踪中发布的gmail密码