How can I use Python's imaplib to list all email folders and read emails from the inbox?

Use `imaplib.IMAP4_SSL` to connect securely, then `M.list()` to obtain folder names (the default inbox name is 'INBOX'). To read emails, call `M.select('INBOX')`, perform a search with `M.search(None, 'ALL')` to get email sequence numbers, and retrieve each email with `M.fetch(num, '(RFC822)')`. The response provides the raw email data. For reading other folders like Exchange's 'Sent Items', use `M.select('"Sent Items"')`. This approach is part of the automation method covered in the [Penetration Basics - Reading Emails Using the IMAP Protocol](/news/penetration-basics-reading-emails-using-the-imap-protocol) article.