代码应该不仅仅可以收取163邮箱,163做了三方软件客户端收取代码验证。最近Python熟练上手,分享下:
def connect_163_mail(self, user, passwd, port , host):
try:
imaplib.Commands["ID"] = ("AUTH")
imapclient = imaplib.IMAP4_SSL(port = port, host = host)
imapclient.login(user, passwd)
args = ("name", "starmail", "contact", user, "version", "1.0.0", "vendor", "myclient")
typ, dat = imapclient._simple_command('ID', '("' + '" "'.join(args) + '")')
print(imapclient._untagged_response(typ, dat, 'ID'))
status, msgs = imapclient.select()
except Exception as e:
print(e)
_msg = "163Mail Login Error : {} 请登录后台设置->邮件收取->开启 邮箱:{}".format(e, user)
print(_msg)
# exit()
return False
_b = msgs[0]
_int_mail_select_num = int(_b.decode())
print("mail: {} count: {}".format(user, _int_mail_select_num))
# exit()
if _int_mail_select_num > 0:
resp, data = imapclient.search(None, "ALL")
for num in data[0].split():
typ, data = imapclient.fetch(num, '(RFC822)')
import email
_raw_email = data[0][1]
email_mes = email.message_from_string(_raw_email.decode())
print(email_mes['Subject'])
print("----end------------------------------------------------------")
imapclient.close()
imapclient.logout()