Python Network Programming POP3
Python Network Programming POP3
The POP3 protocol is an email protocol used to download information from an email server. This information can be stored locally.
Key Points
- POP is an application layer Internet standard protocol.
-
Because POP supports offline access to email, it requires less Internet access.
-
POP does not allow for search facilities.
-
In order to access information, it is necessary to download it.
-
It only allows the creation of a mailbox on the server.
-
It is not suitable for accessing non-email data.
-
POPs are often abbreviated to three or four letter codes. For example: STAT.
POP Commands
The following table describes some POP commands.
S.N. | Command Description |
---|---|
1 | LOGIN This command opens a connection. |
2 | STAT This command displays the number of messages in the current mailbox. |
3 | LIST This command retrieves a summary of messages, displaying a summary of each message. |
4 | RETR This command helps select a mailbox to access emails. |
5 | DELE is used to delete messages. |
6 | RSET is used to reset the session to its initial state. |
7 | QUIT is used to log out of the session. |
Pyhton’s poplib module provides classes called pop() and pop3_SSL() to implement this requirement. We provide the host name and port number as parameters. In the following example, we connect to a gmail server and retrieve messages after providing login credentials.
import poplib
user = 'username'
# Connect to the mail box
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995')
Mailbox.user(user)
Mailbox.pass_('password')
NumofMessages = len(Mailbox.list()[1])
for i in range(NumofMessages):
for msg in Mailbox.retr(i+1)[1]:
print msg
Mailbox.quit()
When the above program is run, the messages will be retrieved.