Python has classes that can be used to send mail messages (SMTP library) and receive and view messages (IMAP and Email libraries). Last week, I demonstrated how to send messages with Python using the SMTP library and its commands. Today, I will demonstrate how to view the message from our python application using Jupyter Notebook in Visual Studio Code.
Fig 1 shows an email (Gmail) with the subject “Hello World“. We are going to write an application to display the content of this mail by using the subject.

Before we start, it is imperative that:
- You have a steady internet connection
- You must have activated your email application to allow less secure apps option on (or generated an App Password from Google)
- You have Jupyter Notebook installed in Visual Studio Code
If you have all that, then open your Visual Studio Code and create a new Jupyter Notebook by opening the Command Palette (CTL + SHIFT + P for windows or CMD + SHIFT+ P if you are using Mac).
1) Select Create New Jupyter Notebook. Save it with any name of your choice and import the necessary libraries as shown in fig 2

The imaplib is a python package that contains classes and methods to help create connections to the mail server for viewing and searching mails. For more information on the imaplib, please visit the imaplib python documentation.
The Email package is used for handling, parsing and generating email messages. It is mainly made for viewing or searching mail messages (not sending messages to an SMTP server). For more information on the Email library, please visit the Email and Mime handling Package python documentation.
2) Run the cell (red arrow in fig 1) and click the code button (labelled 1 in fig 1) to create the next cell
3) Create the connection object to connect to the Gmail IMAP server.

The IMAP4_SSL(‘imap.gmail.com’) connects to the imap server via an encrypted SSL (Secure Socket Layer) connection. This ensures that any mail to be searched or viewed via the internet would be encrypted. Execute the cell ( it’s cell 2 from fig 3). On successful execution, click the code button to generate the next cell.
4) Supply your email address and password using the input statement. Make sure to type your mail address and password correctly (Visual Studio Code will display a textbox above the user interface where you may have to type each twice) and hit the enter key.

The Gmail IMAP Server will authenticate the login credentials (using the login method) and will display the message shown in Fig 4 (cell 4)
5) In Fig 5, the list method shows the different folders inherent in the mail including Inbox, All mail, drafts, spam etc.

6) Since we want to check the message in the inbox folder, we have to use the select method to inform the server that this is the folder we want to search for the content.

7) In Cell 7 (fig 7), we search for the email with the subject “Hello World” as shown earlier in Fig 1.

8) In Cell 10 (fig 8), we assign the data (email content) to the email id variable and used the variable to the field method of the connection object. This method fetches the content using the internet standard protocol – RFC822.

The RFC822 is the standard for the format of internet, text messages including email messages. This standard also requires that any application requesting any output from the mail server must adhere to the standard.
RFC stands for Request for Comments which is a formal document from the Internet Engineering Task Force (IETF) that is the result of the committee drafting and subsequent review by interested parties – From Techtarget
For more information on RFC822, visit the w3 article and/or RFC Editor
9) At this point, the mail content is in a raw state which must be decoded using the utf-8 (cell 12).
10) Finally, we convert the decoded mail message to a string by using the message_from_string method of the email class.

The full code is shown below:
import imaplib
import email
import getpass
mailObject=imaplib.IMAP4_SSL('imap.gmail.com')
my_email=input(getpass.getpass("Your Email goes Here: ")
my_password=input(getpass.getpass("Your password goes here: ")
mailObject.login(my_email, my_password)
#An output similar to the on in Fig 4 would be displayed
#To check the folders to search for in an email, use the list method
mailObject.list()
#To select the inbox folder, use the select method
mailObject.select()
#Output from the server would be displayed here
typ, data= mailObject.search(None, 'SUBJECT "Hello World"')
typ
#output 'OK'
data
#output [b '28493']. Yours may be different
my_email_id=data[0]
result, my_email_data=mmailObject.fetch(my_email_id, '(RFC822)')
the_raw_email=my_email_data[0][1]
the_raw_email_string=the_raw_email.decode('utf-8')
email_message=email.message_from_string(the_raw_email_string)
for part in email_message.walk():
if part.get_content_type() == 'text/plain':
body=part.get_payload(decode=True)
print(body)



