Receiving via POP3
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
line-based communications
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
poplib
.
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
imaplib
and its associated documentation.
There are three steps to establishing a connection to a POP3 mailbox:
Connect to the server by creating a
poplib.POP3()
instance, specifying the hostname.Send the mailbox account name, using the
user()
method.Send the mailbox password using the
pass_()
method (pass
is a reserved word 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
quit()
method. If you don’t unlock the mailbox, other mail clients (such as your regular ...
Get Python Programming On Win32 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.