Sat Aug 04, 2012 4:03 pm
import poplib
mailServer = 'pop.gmail.com'
emailID = '[email protected]'
emailPass = 'xxxx'
## open connection to mail server (Secured using SSL)
myEmailConnection = poplib.POP3_SSL(mailServer)
## print the response message from server
print myEmailConnection.getwelcome()
## set email address
myEmailConnection.user(emailID)
## set password
myEmailConnection.pass_(emailPass)
## get information about the email address
EmailInformation = myEmailConnection.stat()
print "Number of new emails: %s (%s bytes)" % EmailInformation
## Reading an email
print "\n\n===\nRead messages\n===\n\n"
## Read all emails
numberofmails = EmailInformation[0]
for i in range(numberOfMails):
for email in myEmailConnection.retr(i+1)[1]:
print email
import imaplib
imap_host = 'imap.gmail.com'
imap_user = '[email protected]'
imap_pass = 'password'
## open a connection
imap = imaplib.IMAP4_SSL(imap_host)
## login
imap.login(imap_user, imap_pass)
## get status for the mailbox (folder) INBOX
folderStatus, UnseenInfo = imap.status('INBOX', "(UNSEEN)")
print folderStatus
NotReadCounter = int(UnseenInfo[0].split()[2].strip(').,]'))
print NotReadCounter
## create a new folder
status, createFolder_response = imap.create('myFolders.xyz')
## folders list
status, folder_list = imap.list()
## list sub-folders
status, sub_folder_list = imap.list(directory='insd')
## select a specific folder
status, data = imap.select('INBOX')
## searching current folder using title keywords
status, messages = imap.search(None, '(SUBJECT "Work Report")')
## fetching message header by using message( ID)
status, msg_header = imap.fetch('1', '(BODY.PEEK[HEADER])')
## fetching the full message ( ID=1)
status, AllTheMessage= imap.fetch('1', '(RFC822)')
## moving/copying messages around folders
status, messages = imap.copy(msg_ids, 'myFolders.xyz')
status, messages = imap.move(msg_ids, 'otherFolder')
Tue Aug 07, 2012 9:09 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.