公司的邮箱只有50M,时常需要清理,于是写了个清理工具:
import os import time import logging import configparser from exchangelib import DELEGATE, Account, Credentials, Configuration, NTLM, Build, Version from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter import warnings class ngmail(object): def __init__(self): warnings.filterwarnings('ignore') config = configparser.ConfigParser() isread = config.read('ngmail.ini') self.server='mail.xxx.cn' if len(isread)==0: username = input("请输入邮箱用户名: ") pwd = input("请输入邮箱密码: ") config.add_section('userinfo') config.set('userinfo', 'username', username) config.set('userinfo', 'password', pwd) f = open('ngmail.ini', "w") config.write(f) f.close() self.username=username self.password=pwd else: self.username = config['userinfo']['username'] self.password = config['userinfo']['password'] def deleteMail(self): isdelete =input("确定删除所有邮件?(y/n),输入y并回车将删除所有邮件 ") if isdelete=='y' or isdelete=='Y': BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter version = Version(build=Build(14, 3, 123, 0)) credentials = Credentials(username=self.username, password=self.password) config = Configuration( server=self.server, credentials=credentials, version=version, auth_type=NTLM ) # NTLM account = Account(primary_smtp_address=self.username, config=config, access_type=DELEGATE) try: status_report = account.inbox.all().delete() print('成功清空邮箱') time.sleep(1) except Exception as e: if e.value.find("Wrong username")>-1: print('用户名或密码不正确') os.remove('ngmail.ini') self.__init__() self.deleteMail() else: raise e if __name__=='__main__': try: tt= ngmail() tt.deleteMail() except Exception as e: logging.exception(e) os.system('pause')
使用exchangelib的时候,可以指定exchange的版本,从而减少没必要的探测,详细使用规则可以查看官方文档:https://pypi.org/project/exchangelib/