邮件的服务器MTA:smtp协议
邮件的客户端MUA: pop3协议,imap协议。
###############################################
python使用smtplib模块编写邮件服务器程序。
SMTP类:
__init__(self, host=”, port=0, local_hostname=None, timeout=<object object>)
smtplib.SMTP类的方法:
connect(host=’localhost’, port=0)
sendmail(from_addr, to_addrs, msg, mail_options=[], rcpt_options=[]):
to_addrs是一个地址列表,msg的From: To: Subject:能自动识别
sendmail返回没有发出的邮件和错误信息的字典{email,message}
quit()
################################################
python的邮件客户端模块有poplib和imaplib两种
POP3类:
__init__(self, host, port=110, timeout=<object object>)
POP3类的方法:
user(username)
pass_(password)
stat()-> 获取消息的数量和消息总大小,返回(message count, mailbox size)
list(which=None) ->
如果没有给出which返回,,[‘response’, [‘mesg_num octets’, …], octets]
如果给出which只返回response
retr(which)-> 获取which的服务器返回信息、消息的所有行、消息的字节,返回[‘response’, [‘line’, …], octets]
dele(which) -> 删除which,返回response
quit()
response:是一个字符串: +OK …
[‘line’, …]:是一个列表,列表中‘’前面的是服务器返回的消息内容,后面的是邮件正文内容。
####################################################
#!/usr/bin/env pythonfrom smtplib import SMTPfrom poplib import POP3from time import sleepSMTPSVR = ‘smtp.yourmail’POP3SVR = ‘pop3.yourmail’#send to whoto_address = [‘sendtowho’]#this will be auto recognisedorigHdrs = [‘From: youremail’, ‘To: sendtowho’, ‘Subject: test’]#this is the real message you want to sendorigBody = [‘msgline1’, ‘msgline2’, ‘msgline3’]#the origHdrs and the origBody has a null lineorigMsg = ‘\r\n\r\n’.join([‘\r\n’.join(origHdrs),’\r\n’.join(origBody)])#used for send mail with smtpsendSvr = SMTP(SMTPSVR)errs = sendSvr.sendmail(‘yourmail’, to_address, origMsg)sendSvr.quit()assert len(errs) == 0, errs#wait for the mail to comesleep(10)#used for recve mail with poprecvSvr = POP3(POP3SVR)recvSvr.user(‘yourname’)recvSvr.pass_(‘yourpassword’)rsp, msg, siz = recvSvr.retr(recvSvr.stat()[0])sep = msg.index(”)recvBody = msg[sep+1:]assert origBody == recvBody
并且为之实践了关怀和付出的善举。对于我性情中的易感和怨薄,