Python 3 | Send email — SMTP — gmail — Error : SMTPException

∥☆過路亽.° 提交于 2020-02-03 08:22:05

问题


I want to send an email by use of Python 3. I cannot yet make sense of the examples that I have seen. Here is one reference: Using Python to Send Email

I have pulled the first simple example found on the above reference. I find this example a good representation of the combination of examples I have seen on the internet. It seems to be the basic form of doing what I am trying to do.

When I try the code below, I receive Error:

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.

Here is the code:

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

回答1:


I have found a solution on YouTube.

Here is the 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()



回答2:


As of mid-October 2017, gmail isn't accepting connections via smtplib.SMTP() on port 587, but requires smtplib.SMTP_SSL() and port 465. This starts TLS immediately, and ehlo isn't needed. Try this snippet instead:

# 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.



回答3:


This is how I sent an email using Google. Capital letters represent personal information that needs to be edited

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



回答4:


This function is working for me:

`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
#--------
`

I hope this helps.



来源:https://stackoverflow.com/questions/17332384/python-3-send-email-smtp-gmail-error-smtpexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!