Python receive mail sample
Import Base64
Import Imaplib
From Email.mime.text import Mimetext
Import Email
Import Base64
import imaplib from
email.mime.text import mimetext Import
Email
def get_first_unread_email_title_body (self): "" Gets the first title and content return value for unread messages: Subj: The title of the first message, no message returns none Bdy: The contents of the first message, no message returns none "" "conn = Imaplib. Imap4_ssl ("smtp.xxxx.com.cn", 993) Conn.login ("username", "password") conn.select () typ, data = Co
Nn.search (None, ' unseen ') subj =[] bdy=[] try:for num in Data[0].split (): Typ, msg_data = Conn.fetch (num, ' (RFC822) ') for Response_part in Msg_data:if ISI
Nstance (Response_part, tuple): msg = email.message_from_string (response_part[1]) #获取消息体 subject=msg[' subject '] #print subject Sub J.append (subject) Payload=msg.get_payload () Body=self.extract_body (payload ) #lens = Len (body) #print lens, "message leNgth "#lenx = lens-(lens%4 if lens%4 else 4) #try:
#result = base64.decodestring (Body[:lenx]) #except: #pass Print body#base64.decodestring (body) bdy.append Typ, RESPO
NSE = Conn.store (num, ' +flags ', R ' (\seen) ') finally:print "finished!" Try:conn.close () Except:pass conn.logout () If Len (bdy) &G
T;0:print Subj[0],bdy[0] return subj[0],bdy[0] else:print ' none ', ' none ' Return ' None ', ' none '