Python 3 | 发送电子邮件 -- SMTP -- Gmail -- 错误:SMTPException

5
我希望能够用Python 3发送电子邮件,但我还无法理解我看到的示例。这是一个参考:使用Python发送电子邮件
我提取了上述参考中找到的第一个简单示例。我认为这个示例很好地代表了我在互联网上看到的示例的组合。它似乎是我尝试做的基本形式。
当我尝试下面的代码时,我收到错误:
File "C:\Python33\Lib\email.py", line 595, in login
    raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.

这是代码示例:
# Send Mail

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)

# Log in to the server
server.login("myEmail@gmail.com","myPassword")

# Send mail
msg = "\nHello!"
server.sendmail("myEmail@gmail.com","recipient@gmail.com", msg)
4个回答

18
我在YouTube上找到了解决方案。
这是视频链接: video link
# smtplib module send mail

import smtplib

TO = 'recipient@mailservice.com'
SUBJECT = 'TEST MAIL'
TEXT = 'Here is a message from python.'

# Gmail Sign In
gmail_sender = 'sender@gmail.com'
gmail_passwd = 'password'

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)

BODY = '\r\n'.join(['To: %s' % TO,
                    'From: %s' % gmail_sender,
                    'Subject: %s' % SUBJECT,
                    '', TEXT])

try:
    server.sendmail(gmail_sender, [TO], BODY)
    print ('email sent')
except:
    print ('error sending mail')

server.quit()

6
截至2017年10月中旬,gmail不再接受通过端口587的smtplib.SMTP()方式连接,而要求通过端口465的smtplib.SMTP_SSL()方式连接。这将立即启动TLS,且无需ehlo。请尝试使用以下代码段:
# Gmail Sign In
gmail_sender = 'sender@gmail.com'
gmail_passwd = 'password'

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(gmail_sender, gmail_passwd)

# build and send the email body.

刚试了一下,我想知道我可以在本地使用这个东西,但不能从我的服务器上使用,因为谷歌仍然说 请通过您的 Web 浏览器登录\n5.7.14 然后重试。\n5.7.14 了解更多信息:\n5.7.14 https://support.google.com/mail/answer/78754 - gies0r
你似乎是错的。截至2020年3月28日,Gmail似乎正在端口587上接受邮件。但是,您需要将此Gmail帐户启用为“不安全”帐户,而这被Gmail安全监控视为漏洞。但是,您仍然可以通过“不安全”的方式发送邮件,在我的系统中实际上可以正常工作,消息可以通过端口587发送并到达目标邮箱。 - Eugene Gr. Philippov
@gies0r,您需要在Gmail帐户设置中禁用双重身份验证才能使其正常工作。 - Eugene Gr. Philippov
但是我使用Java SSL向587发送邮件,一切似乎都正常。当尝试在端口587上使用smtplib.SMTP_SSL登录时,Python v.3.6.9报告“ssl.SSLError: [SSL:WRONG_VERSION_NUMBER] wrong version number”的错误。而Java Oracle JDK没有报告这个错误。 - Eugene Gr. Philippov
当使用SSL登录端口465时,如果IP地址是Gmail未知的新地址,则会出现smtplib.SMTPAuthenticationError: (534, b'5.7.14 <...\n5.7.14 ...\n5.7.14 ...>\n5.7.14 请通过您的Web浏览器登录,然后重试。\n5.7.14 了解更多信息,请访问\n5.7.14 https://support.google.com/mail/answer/78754 ... - gsmtp')。您需要前往Gmail帐户设置并确认此IP地址是安全的,之后您就可以使用Python API登录并发送邮件。 - Eugene Gr. Philippov

3

这是我使用Google发送电子邮件的方法。大写字母代表需要编辑的个人信息。

try:
    import RUNNING_SCRIPT
except:
    print("threw exception")
    # smtplib module send mail

    import smtplib

    TO = ‘EXAMPLE_RECIPIENT@gmail.com'
    SUBJECT = 'SERVER DOWN'
    TEXT = 'Here is a message from python. Your server is down, please check.'

    # Gmail Sign In
    gmail_sender = ‘YOUR_GMAIL_ACCOUNT@gmail.com'
    gmail_passwd = ‘APPLICATION SPECIFIC PASSWORD’

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login(gmail_sender, gmail_passwd)

    BODY = '\r\n'.join(['To: %s' % TO, 'From: %s' % gmail_sender,'Subject: %s' % SUBJECT,'', TEXT])

    try:
        server.sendmail(gmail_sender, [TO], BODY)
        print ('email sent')
    except:
        print ('error sending mail')
        server.quit()

1

这个函数对我有效:

`def server_connect(account, password, server, port=587):
    if int(port) == 465:    # gmail服务器
        email_server = smtplib.SMTP_SSL(server, str(port))
    else:
        email_server = smtplib.SMTP(server, port)
        email_server.ehlo()
        email_server.starttls()
    email_server.login(account, password)
    return email_server
#--------
`

希望这可以帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接