简体   繁体   English

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

[英]How Do I Send Emails Using Python 3.4 With GMail?

I've been trying bits of code such as: 我一直在尝试一些代码,例如:

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')

Except with an actual password and details. 除非带有实际密码和详细信息。

It brings this error: 它带来此错误:

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

Ideally, I could know what's wrong with the code and how I can fix this. 理想情况下,我可以知道代码有什么问题以及如何解决。 If it's totally wrong then some working code with Gmail would be greatly appreciated. 如果完全错误,那么不胜感激Gmail的一些工作代码。

Thanks! 谢谢!

You have to split the smpt server address and the port and use them as separate parameters for smtplib.SMTP('smtp.gmail.com', 587) See https://docs.python.org/3.4/library/smtplib.html : 您必须拆分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')

By the way: change your gmail password as it was posted in the stacktrace 顺便说一句:更改您的Gmail密码,该密码已在stacktrace中发布

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

相关问题 如何使用Python通过Gmail发送电子邮件 - How to Send Emails with Gmail using Python 如何通过 python 的 gmail-api 发送 HTML 格式的电子邮件 - How do I send HTML Formatted emails, through the gmail-api for python 如何通过应用程序密码通过 gmail 发送电子邮件? - How do I send emails through gmail via app passwords? 如何使用python向我的gmail发送电子邮件? - How do I send an email to my gmail using python? 您如何访问gmail收件箱并使用python管理电子邮件? - How do you accsess to a gmail inbox and manage emails using python? 使用 python 3 和 Gmail API 发送带有附件的电子邮件,我最终遇到文件损坏或 ConnectionAbortedError - Using python 3 and Gmail API to send emails with attachments, I end up with either corrupted files or ConnectionAbortedError 使用python和gmail在同一线程中发送多封电子邮件 - Send multiple emails in the same thread using python and gmail 如何使用 Python 在 Outlook Express 中以编程方式发送加密电子邮件? - How do I programmatically send encrypted emails in Outlook Express using Python? 如何在 python 中的 outlook 上向多个人发送电子邮件? - How do I send emails to multiple people on outlook in python? 我现在如何(自 2022 年 6 月起)使用 Python 脚本通过 Gmail 发送 email? - How do I now (since June 2022) send an email via Gmail using a Python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM