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

时间:2013-06-26 23:57:31

标签: email python-3.x smtp-auth

我想使用Python 3发送电子邮件。我还不能理解我所见过的例子。以下是一个参考:Using Python to Send Email

我已经在上面的参考资料中找到了第一个简单的例子。我发现这个例子很好地代表了我在互联网上看到的各种例子。这似乎是我正在做的事情的基本形式。

当我尝试下面的代码时,我收到错误:

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 个答案:

答案 0 :(得分: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()

答案 1 :(得分:5)

截至2017年10月中旬,gmail未通过smtplib.SMTP()端口587接受连接,但需要smtplib.SMTP_SSL()和端口465。这会立即启动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.

答案 2 :(得分:1)

这就是我使用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()

答案 3 :(得分:0)

此功能对我有用:

`def server_connect(account, password, server, port=587):
    if int(port) == 465:    # gmail server
        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
#--------
`

我希望这会有所帮助。