尝试使用python连接到Exchange邮件帐户时出现错误“无法创建密钥为..的缓存协议”

2024-05-20 22:39:24 发布

您现在位置:Python中文网/ 问答频道 /正文

你好。我有一个python脚本,用于检查邮箱中的未读邮件。如果有带附件的未读邮件,则会执行一些代码(excel自动化)。它过去运作良好;但是,今天我在执行脚本时遇到了以下错误:

Failed to create cached protocol with key ('********/EWS/Exc hange.asmx', Credentials('*******', '********')): HTTPSConnectio nPool(host='*******', port=443): Max retries exceeded with url: /EW S/Exchange.asmx (Caused by SSLError(SSLCertVerificationError("hostname 'mail.example.com' doesn't match either of '*.example.com', 'example.com'")))

其中example.com是公司的服务器

我搜索了一些答案,发现一个adivce说我应该将veryfy ssl设置为False(Account)。当我这样做时,我不再从命令行收到错误,但脚本只在1秒内完成,什么也不做

这里有什么问题?我没有对代码做任何更改,所以这是邮件服务器的错误吗? 以下是从邮件中获取附件的代码片段:

def get_attachments(login, password, path):
    files_list = []
    credentials = Credentials(
    username=login,  
    password=password
    )
    config = Configuration(server=settings.server, credentials=credentials)
    account = Account(primary_smtp_address=login, 
    config=config, autodiscover=False, access_type=DELEGATE)
    unread = account.inbox.filter(is_read=False) 
    attachment_counter = 0
    for msg in unread:
        msg.is_read = True
        msg.save()
        for attachment in msg.attachments:
            fpath = os.path.join(path, attachment.name)
            if os.path.exists(fpath):
                attachment_counter += 1
                fpath = os.path.join(path, (str(attachment_counter) + attachment.name))
            if attachment.name.split(".")[-1].lower() in ['xlsx', 'xls']:
                with open(fpath, 'wb') as f:
                   f.write(attachment.content)
                   files_list.append(fpath)
    return files_list

Tags: path代码脚本comfalseattachmentexample错误
1条回答
网友
1楼 · 发布于 2024-05-20 22:39:24

错误是由Exchange服务器上安装的SSL证书无效引起的-该证书对于提供该证书的域名无效。您可以自行禁用SSL验证。请参阅https://github.com/ecederstrand/exchangelib/blob/master/README.md#proxies-and-custom-tls-validation上的操作说明

您的脚本不打印任何输出,因此实际上不可能知道它是否按预期工作。在快速Exchange服务器上,1秒足以检查收件箱中的未读电子邮件。也许你只是没有任何未读的电子邮件,或者没有带Excel附件的未读电子邮件

相关问题 更多 >