669.Python收发邮件

收邮件,邮件正文可能是base64编码也可能是gb2312编码,如何始终获取正文呢

import poplib,email
try:
    p=poplib.POP3('pop.qq.com')
    p.user('xx')
    p.pass_('xx')
    ret = p.stat()
except poplib.error_proto,e:
    print "Login failed:",e
list=p.list()[1]
list.reverse()
for item in list:
    number,octets = item.split(' ')
    lines=p.retr(number)[1]
    for piece in lines:
        print piece

发邮件:

import smtplib
try:
    handle = smtplib.SMTP('smtp.163.com', 25)
    handle.login('[email protected]','xx')
    msg = "To:[email protected]\r\nFrom:[email protected]\r\nSubject:hi https://salangsayang.com/ \r\n\r\nhello\r\n"
    handle.sendmail('[email protected]','[email protected]', msg)
    handle.close()
except Exception,e:
    print e

Author: 搬运工

发表评论