# Lesson 14 email error - need help

**URL:** https://forum.codewithmosh.com/t/lesson-14-email-error-need-help/14617
**Category:** Python
**Created:** [August 22, 2022, 1:17pm UTC](https://forum.codewithmosh.com/t/lesson-14-email-error-need-help/14617 "2022-08-22T13:17:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Raidenbond](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Raidenbond](https://forum.codewithmosh.com/u/Raidenbond)
#### Post date: [August 22, 2022, 1:17pm UTC](https://forum.codewithmosh.com/t/lesson-14-email-error-need-help/14617/1 "2022-08-22T13:17:32Z")

</div>

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](mailto:raidenjason99@gmail.com)”  
message[“subject”] = “This is a test”

message.attach(MIMEText(“Body”))

with smtplib.SMTP(host=“[smtp.gmail.com](http://smtp.gmail.com)”, port=587) as smtp:

smtp.ehlo()  
smtp.starttls()  
smtp.login(“[jasonmengma08@gmail.com](mailto:jasonmengma08@gmail.com)”,“Water1885$”)  
smtp.send\_message(message)  
print(“Sent…”)

Thank you for the help!

 ![Screenshot 2022-08-22 091054](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/3/35a3068ca57207a870bce7d9b874861932623f5b.jpeg)  
 ![Screenshot 2022-08-22 091142](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/3/3156437ec8ecbbc474a6414571aeec1366056243.jpeg)

---

<div class="post-metadata">

### Author: ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)
#### Post date: [August 22, 2022, 4:11pm UTC](https://forum.codewithmosh.com/t/lesson-14-email-error-need-help/14617/2 "2022-08-22T16:11:48Z")

</div>

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:

> **[Sign in using app passwords - Gmail Help](https://support.google.com/mail/answer/185833?hl=en-GB)**
>
> An app password is a 16-digit passcode that gives a non-Google app or device permission to access your Google Account. App passwords can only be used with accounts that have 2-Step Verification turned

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

---

<div class="post-metadata">

### Author: ![Raidenbond](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Raidenbond](https://forum.codewithmosh.com/u/Raidenbond)
#### Post date: [August 22, 2022, 10:25pm UTC](https://forum.codewithmosh.com/t/lesson-14-email-error-need-help/14617/3 "2022-08-22T22:25:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)
#### Post date: [August 22, 2022, 10:29pm UTC](https://forum.codewithmosh.com/t/lesson-14-email-error-need-help/14617/4 "2022-08-22T22:29:04Z")

</div>

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 ```

```auto
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.

---

<div class="post-metadata">

### Author: ![Raidenbond](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Raidenbond](https://forum.codewithmosh.com/u/Raidenbond)
#### Post date: [August 22, 2022, 10:49pm UTC](https://forum.codewithmosh.com/t/lesson-14-email-error-need-help/14617/5 "2022-08-22T22:49:57Z")

</div>

hi @jmrunkle , is like this?

```auto
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!

---

<div class="post-metadata">

### Author: ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)
#### Post date: [August 22, 2022, 11:54pm UTC](https://forum.codewithmosh.com/t/lesson-14-email-error-need-help/14617/6 "2022-08-22T23:54:29Z")

</div>

> [@Raidenbond](#):
>
> The 2 FA meaning like I have two gmail accounts can not be use in the message and smtp to send to client?

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.
