python 自动监控最新邮件并读取的操作
我就废话不多说了,大家还是直接看代码吧~
#zmail库:可以用几行代码帮我们收取一封邮件
importzmail
#输入账号和密码
server=zmail.server('13163964546@qq.com','jie110341')
#获取最新的一封邮件
mail=server.get_latest()
#读取邮件
#zmail.show(mail)
#读取邮件的部分内容
print(mail['subject'])
......
#读取附件邮件存放路径如果有同名文件则覆盖
zmail.save_acctachment(mail,target_path=None,overwrite=True)
需要在电脑上下载zmail库
补充:Python邮箱实施监控电脑
我就废话不多说了,大家还是直接看代码吧~
importsmtplib
importpoplib
importemail
fromemail.mime.applicationimportMIMEApplication
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.headerimportdecode_header
defsend_email(account,password,email_title,send_text="",file_names=None,file_dir="."):
msg=MIMEMultipart()
#msg=MIMEText(HTML,'html')--只能发送文本内容
content=MIMEText(send_text,"plain","utf-8")
msg.attach(content)
#文件类型
ifisinstance(file_names,list):
forfile_nameinfile_names:
send_file_path=file_dir+"/"+file_name
part=MIMEApplication(open(send_file_path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=file_name)
msg.attach(part)
elifisinstance(file_names,str):
send_file_path=file_dir+"/"+file_names
part=MIMEApplication(open(send_file_path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=file_names)
msg.attach(part)
#msg['from'],msg['to']接收端显示的发件人与收件人
msg['from']="奥巴马@163.com"
msg['to']=account
msg['subject']=email_title
try:
server=smtplib.SMTP()
server.connect('smtp.163.com')
server.login(account,password)
#from_addr:发送地址;to_addrs:接收地址(字符串列表)
server.sendmail(account,msg['to'].split(),msg.as_string())
exceptExceptionase:
print(e)
#获取邮件标题
defget_email_subject(addr,password):
#设置连接网址,获取pop3协议的邮件读取对象
read=poplib.POP3('pop.163.com',timeout=3600)
#输入邮件地址与邮件登录密码
read.user(addr)#163邮箱用户名
read.pass_(password)#163邮箱设置中的客户端授权密码
#allEmails=(totalNum,totalSize)
#读取邮件信息(邮件总数,邮件尺寸)
total_num,total_size=read.stat()
#top(which,howmuch)
#获取最新的一封邮件(第几封邮件,获取多少封)
top_email=read.top(total_num,1)
#print("*****start*****\n接收的数据为:{}\n*****end*****\n".format(top_email))
#
#print("*****start*****\n[解码前]获取的初始邮件内容:{}\n*****end*****\n".format(top_email[1]))
#解码邮件信息,将解码后的邮件信息存入tmp
tmp=[]
forsintop_email[1]:
tmp.append(s.decode())
#print("*****start*****\n[解码后]的邮件内容为:{}\n*****end*****\n".format(tmp))
#将解码后的邮件内容拼接为字符串
email_str='\n'.join(tmp)
#将字符串类型解析为Message类型
message=email.message_from_string(email_str)
#print("*****start*****\n"
#"[解码前]的邮件字符串内容为:[数据类型]{}\n{}\n"
#"--------------------------------------------\n"
#"[解码后]的邮件字符串内容为:[数据类型]{}\n{}\n"
#"*****end*****\n"
#.format(type(email_str),email_str,type(message),message))
#获取邮件主题
subject_str=message['subject']
#print("*****start*****\n[解码前]邮件标题:{}\n*****end*****\n".format(subject_str))
subject=decode_header(subject_str)
#print("*****start*****\n[解码后]邮件标题:{}\n*****end*****\n".format(subject))
content=subject[0][0]
enc_type=subject[0][1]
ifenc_type:
subject_decode=content.decode(enc_type)
else:
subject_decode=content
returnsubject_decode,read,total_num
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。