POP3 downloads messages from a remote mailbox. As we discussed previously, SMTP is used typically to send Internet email messages, and POP3 receives them.
Like most Internet protocols, POP3 uses a
protocol, and also like most Internet protocols, you will find a
Python module designed to ease working with that protocol; in this
case the Python module is
Before delving into a discussion of POP3, it is worth noting that an
improved protocol known as Internet Message Access
Protocol (IMAP) has been designed. Although this fixes many of the
shortcomings in the POP3 protocol, it’s not used as widely as
POP3. Therefore, we will discuss using POP3 to ensure the code works
on the widest possible range of mail servers. If you need to
investigate using the IMAP protocol, you should view the module
and its associated documentation.
There are three steps to establishing a connection to a POP3 mailbox:
Connect to the server by creating a
instance, specifying the hostname.
Send the mailbox account name, using the
Send the mailbox password using the
pass is a
in Python, hence the trailing underscore).
You now have a valid connection, and the mailbox is locked. While the
mailbox is locked, no other connections are possible, so it’s
important to unlock the mailbox when you’re done using the
method. If you don’t unlock the mailbox, other mail clients (such as your regular ...