Tutorialspoint

1 Answer
Rishi Raj

The poolib module from Python's standard library defines POP3 and POP3_SSL classes. POP3 class encapsulates a connection to a POP3 server and implements the protocol as defined in RFC 1939. POP3_SSL classsupports POP3 servers that use SSL as an underlying protocol layer.

POP3 protocolis obsolescent as its implementation quality of POP3 servers is quite poor. If your mailserver supports IMAP, it is recommended to use the imaplib.IMAP4 class.

Both classes have following methods defined −

getwelcome()

Returns the greeting string sent by the POP3 server.

user(username)

Send user command, response should indicate that a password is required.

pass_(password)

Send password.

Stat()

Get mailbox status. The result contains 2 integers: (message count, mailbox size).

list()

Request message list, result is in the form (response, ['mesg_num octets', ...], octets).

retr()

Retrieve message of specified index, and set its seen flag.

Dele()

Flag message number which for deletion.

Top()

Retrieves the message header plus number of lines of the message after the header of message

quit(): Signoff

commit changes, unlock mailbox, drop connection.

Example

Following code retrieves all unread messages from gmail’s POP server.

import poplib
box = poplib.POP3_SSL('pop.googlemail.com', '995')
box.user("YourGmailUserName")
box.pass_('YourPassword')
N = len(box.list()[1])
for i in range(N):
   for msg in box.retr(i+1)[1]:
      print (msg)
box.quit()

Before running above script ensure that your gmail account is configure to allow less secure apps.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.