Sending emails in python

Hi All,

I have written the following python program for sending email. The code is same as was explained in the course video but it is not working as expected and throwing errors. Please see what corrections are required in this script.

###################################################

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

message = MIMEMultipart()
message[“from”] = “Mosh Hamedani”
message[“to”] = “mohammed.ferozkhan@gamil.com
message[“subject”] = “This is a test”
message.attach(MIMEText(“Body”))

with smtplib.SMTP(host=“smtp.gmail.com”, port=587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.login(“username”, “passwd”)
smtp.send_message(message)
print(“Sent…”)

##############################################
Errors:

Traceback (most recent call last):
File “c:\D Drive\Mohammed\Projects\Python\Python basics\standard-libraries.py\sending-emails.py”, line 26, in
smtp.login(“mohammed.ferozkhan@gmail.com”, “Sm0k3B1ank3t123@”)
File “C:\Users\feroz.khan\AppData\Local\Programs\Python\Python310\lib\smtplib.py”, line 750, in login
raise last_exception
File “C:\Users\feroz.khan\AppData\Local\Programs\Python\Python310\lib\smtplib.py”, line 739, in login
(code, resp) = self.auth(
File “C:\Users\feroz.khan\AppData\Local\Programs\Python\Python310\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 https://support.gooogle.com/mail/?p=BadCredentials a9-20020aa78649000000b0052531985e3esm29130783pfo.22 - gsmtp’)

Im 90% sure the error is saying that the password and username are wrong and not accepted, so it is throwing an error.

Also I can see ur email and password in the 4th line of the error so I would fix that if I were you