-1

I have tried to execute the following program in Python for sending an email. The program is as follows:

Python Code

 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
 from email.mime.image import MIMEImage
 from pathlib import Path
 import smtplib

 message = MIMEMultipart()
 message["from"] = "Sarmad Alsaadi"
 message["to"] = "testpythonmail54@gmail.com"
 message["subject"] = "This is a test"
 message.attach(MIMEText("Body", "plain"))
 with smtplib.SMTP(host="smtp.gmail.com", port=587) as smtp:
   smtp.ehlo()
   smtp.starttls()
   smtp.login("testpythonmail54@gmail.com", "acrobat12345")
   smtp.send_message(message)
   print("Sent........")`

But I am getting the following error

File “e:\Tutorials\Mosh\mastery_course\sending_emails.py”, line 16, in
smtp.login(“testpythonmail54@gmail.com”, “acrobat12345”)

File “C:\Program Files\Python311\Lib\smtplib.py”, line 750, in login
raise last_exception

File “C:\Program Files\Python311\Lib\smtplib.py”, line 739, in login
(code, resp) = self.auth(
^^^^^^^^^^

File “C:\Program Files\Python311\Lib\smtplib.py”, line 662, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b’5.7.8 Username and Password not accepted. Learn more at\n5.7.8 Check Gmail through other email platforms - Gmail Help s7-20020adfeb07000000b002bff1de8d4bsm11043112wrn.49 - gsmtp’)`

I am using VS code with Python extension installed on it. Anything else written in Python get executed successfully and I faced no problems with other programs being executed via the Terminal.

I would so much appreciate it if someone could kindly extend me a helping hand

I found some similar previous topics as mine but they were different in errors produces

Axe319
  • 4,255
  • 3
  • 15
  • 31
Sarmad
  • 21
  • 4
  • 2
    Does this answer your question? [SMTPAuthenticationError when sending mail using gmail and python](https://stackoverflow.com/questions/26852128/smtpauthenticationerror-when-sending-mail-using-gmail-and-python) – 12944qwerty Feb 07 '23 at 16:34
  • 1
    There are a vast array of duplicate questions here; did you research this error prior to posting in accordance with [ask]? Why didn't any of the other questions on this topic address your issue? – esqew Feb 07 '23 at 16:43
  • Yes I searched them and I tried to benefit from them but to no avail, nevertheless I will try again. It may the reason behin this the different version of Python as I am using version 3.11 while the code I am using follows an older version – Sarmad Feb 08 '23 at 09:19

1 Answers1

-1

I'm not 100% sure, but it seems to me that password is incorrect.

CMTP requires special key. You should use password generated for third-party applications. You can add this type of passwords into your Google account by going into Settings -> Security -> App password. Also you will need 2-step verification to do that. So create new key and try to put it in the code. It should work then.

Also be careful about posting any of your personal data (password and other)

  • I have tried every possible way on my Google account but to no avail. Also I am advised not to set up two ways security for accessing my account. Have you ever tried such a program or a similar one? Thanks for trying to help me. – Sarmad Feb 08 '23 at 17:11