-1

enter image description here

So i try smtplib to send gmail automatically, however it fail because of a error, after some checking, my username and pass are True. So i want to ask if there were any mistakes that can lead to this error.

enter image description here

Here is my code, the port is 465 and i don't want anyone know my pass and my username.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 1
    Please edit your question and include [example] and describe any errors you are having. "fail because of a error" does not properly describe the error. Note images are not acceptable – Linda Lawton - DaImTo Jun 22 '22 at 04:23

2 Answers2

0

Create a sendEmail() function, which will help to send emails to one or more than one recipient by calling the function.

def sendEmail(to, content):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('youremail@gmail.com', 'your-password')
    server.sendmail('youremail@gmail.com', to, content)
    server.close()
content = "Message to send"
to = "useremail@gmail.com"    
sendEmail(to, content)

NOTE: Do not forget to make sure that the smtplib requires 'enable the less secure apps' feature in your Gmail account. Otherwise, the sendEmail function will not work properly..

Create App Password: App Passwords

  • 1
    -1 **'enable the less secure apps'** was removed by google May 30, 2022. You can read all about it here [Less secure apps & your Google Account](https://support.google.com/accounts/answer/6010255?hl=en). Suggesting a removed setting as a possible solution to a question is not going to help and will confuse not only the author of this question but anyone in the future who reads your answer. Also as of this time using the actual password for a google account will not work with the smtp server. Your answer is wrong and will not work. Did you test it before posting? – Linda Lawton - DaImTo Jun 22 '22 at 04:32
  • Welcome to stack please read [How to anwser](https://stackoverflow.com/help/how-to-answer) – Linda Lawton - DaImTo Jun 22 '22 at 04:33
  • 1
    I know google was removed this feature but still, I have written this note to make sure the smptlib requires this feature. – Jocefyne_root Jun 22 '22 at 04:43
  • Google removed it as in the setting does not exist anymore. How would you like someone to "Do not forget to 'enable the less secure apps' "? You cant enable something that does not exist. The smtp can not require something that doesn't exist. If you try to use the actual google password you will get a "username and password not accepted" error. Your answer does not address the question it just validates it. – Linda Lawton - DaImTo Jun 22 '22 at 04:44
  • 1
    sorry!! check the note. I suggest you to use other libs of python to send email like: yagmail – Jocefyne_root Jun 22 '22 at 04:54
  • 1
    the easiest way to send email using python to use ```trycourier``` it is very easy to use just visit: ```https://www.courier.com/``` website and follow the instructions. – Jocefyne_root Jun 22 '22 at 05:05
  • 1
    Author asked a question about python smtplib. Your answer should be regarding a solution for smtplib. [How to anwser](https://stackoverflow.com/help/how-to-answer) No matter what lib you use you wont be able to enable less secure apps via your google account. – Linda Lawton - DaImTo Jun 22 '22 at 05:22
  • 1
    everyone knows this enable less secure apps is not more in google so why u stuck on this point. not all libs require ````enable less secure apps``` – Jocefyne_root Jun 22 '22 at 05:41
0

The error message "username and password not accepted" is the standard error message you get from the smtp server when sending the users actual google password.

This method of authentication is not acceptable by the SMTP server after (May 30, 2022). Partially due to the removal of Less secure apps & your Google Account mostly because client login is not considered to be secure.

Your options are to to enable 2fa on our google account and use and create an apps password. Simply use it in place of the password in your code.

smtp.login(username, appsPasswrod)

Or to use XOauth2 and authenticate the application.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449