Lesson 14 email error - need help

Hi Everyone, anyone can assist me who has similar issue here with setting up the email with the code below where all steps are taken correctly:

Sending email

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

message = MIMEMultipart()
message[“from”] = “Jason ma”
message[“to”] = “raidenjason99@gmail.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(“jasonmengma08@gmail.com”,“Water1885$”)
smtp.send_message(message)
print(“Sent…”)

Thank you for the help!


I think this is Gmail’s fun thing where you need to setup an application specific password for doing programmatic access to APIs like this when you are using 2FA. Here is a help article about app specific passwords:

I am sure you could also check out the link they generated for you.

Hi thanks for the reply of the email , so I have to sign in gmail, the code and every other part seems fine, I just need to follow these steps above , it should work?

Thanks @jmrunkle

I do not know for sure that it would work, but if you want someone to look at your code you should probably copy and paste the whole code in a Markdown codeblock by surrounding it with ```

def some_python_code():
  """Like this"""
  print("Hello World")

It looks like it was working and just failed authentication with Gmail because you have 2FA on your account.

1 Like

hi @jmrunkle , is like this?

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

message = MIMEMultipart()
message[“from”] = “Jason ma”
message[“to”] = “raidenjason99@gmail.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(“jasonmengma08@gmail.com”,“Water1885$”)
smtp.send_message(message)
print(“Sent…”)

The 2 FA meaning like I have two gmail accounts can not be use in the message and smtp to send to client? The code seems to be ok I checked without with another post but can you tell if it is ok ? Thanks again!

Not exactly. 2FA stands for Two-Factor Authentication. It is where you get a text message or some other “second factor” for logging into your account.

If you have 2FA enabled on your gmail account, you need to use an app-specific password to be able to login programmatically. You use that in place of your standard password.

The code looks reasonable to me, but I am not intimately familiar with the email.mime package or smtplib.