Python SMTPAuthnticationError

我只是一个虾纸丫 提交于 2020-01-16 17:10:40

问题


Up until yesterday I had no issue sending emails with python and now for some reason I am getting the following error.

Traceback (most recent call last):
  File "main.py", line 20, in <module>
    mail(name)
  File "/home/runner/mail.py", line 26, in mail
    smtpserver.login(gmail_user, gmail_password)
  File "/usr/local/lib/python3.7/smtplib.py", line 730, in login
    raise last_exception
  File "/usr/local/lib/python3.7/smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "/usr/local/lib/python3.7/smtplib.py", line 642, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials p17sm967082ils.71 - gsmtp')

I have been using the below code for about 4 months and haven't had any issues. Here is the code I am using to access gmail:

import smtplib

def mail(name):
    gmail_user = 'email@gmail.com'
    gmail_password = 'password'

    emaillist = ['email@email.com']

    for email in emaillist:
        sent_from = gmail_user
        to = email
        subject = 'Subject'

        body = "Body"

        email_text = '''\
        From: %s
        To: %s
        Subject: %s

        %s
        '''%(sent_from, to, subject, body)

        try:
            server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
            server.ehlo()
            server.login(gmail_user, gmail_password)
            server.sendmail(sent_from, to, body)
            server.close()

            print('Email sent!')
        except:
            print('Oops!')

I have tried changing the use less secure apps in gmail, enabling IMAP, and changing the password for the email account. So far no luck with solving the issue. Any help would be greatly be appreciated.


回答1:


Google has increased its security policies, I invite you to read this link in the part of: Less secure apps & your Google Account and open the link like bellow ( Less secure app access)

https://support.google.com/accounts/answer/6010255

later Allow access to unsafe applications: YES




回答2:


here's my code you need to set the smtp.gmail.com port to 587

At the sendMail we have alarm notification is subject and Someone has entered the room, picture attached is the body we also have an attachment if needed ["/home/pi/webcam.jpg"]

CODE:

#!/usr/bin/env python
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os

USERNAME = "___YOUR SMTP EMAIL HERE___"
PASSWORD = "__YOUR SMTP PASSWORD HERE___"

def sendMail(to, subject, text, files=[]):
    assert type(to)==list
    assert type(files)==list

    msg = MIMEMultipart()
    msg['From'] = USERNAME
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    for file in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload( open(file,"rb").read() )
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"'
                       % os.path.basename(file))
        msg.attach(part)

    server = smtplib.SMTP('smtp.gmail.com:587')
    server.ehlo_or_helo_if_needed()
    server.starttls()
    server.ehlo_or_helo_if_needed()
    server.login(USERNAME,PASSWORD)
    server.sendmail(USERNAME, to, msg.as_string())
    server.quit()

sendMail( ["___EMAIL TO RECEIVE THE MESSAGE__"],
        "Alarm notification",
        "Someone has entered the room, picture attached",
        ["/home/pi/webcam.jpg"] )


来源:https://stackoverflow.com/questions/59270643/python-smtpauthnticationerror

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