zoukankan      html  css  js  c++  java
  • Python操作Mircrosoft Exchange邮箱

    公司的邮箱只有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/

  • 相关阅读:
    mui的相关知识
    4. 本地的json格式调用方法
    DOM树节点的相关知识
    3.函数引用中“值传参”与“引用传参”的区别
    一,数组的创建 数组的遍历
    重载<<
    SendMessage、PostMessage、PeekMessage、GetMessage、PreTreslateMessage等
    TranslateMessage
    怎样在整个类中恒定常量
    格式化输出
  • 原文地址:https://www.cnblogs.com/xienb/p/12840857.html
Copyright © 2011-2022 走看看