首页 > 编程语言 > 详细

Python操作Mircrosoft Exchange邮箱

时间:2020-05-07 09:31:46      阅读:73      评论:0      收藏:0      [点我收藏+]

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

Python操作Mircrosoft Exchange邮箱

原文:https://www.cnblogs.com/xienb/p/12840857.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!