使用 Python 从 Outlook 发送电子邮件不起作用 [英] Sending emails from outlook with Python not working

查看:63
本文介绍了使用 Python 从 Outlook 发送电子邮件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在运行一些代码,它使用 win32com.client 自动处理一些电子邮件.一切都已经工作了几个月,但今天我遇到了一个错误.

I have been running some code for a while which automates some emails using win32com.client. All has been working for months but today I am getting an error.

import win32com.client

olMailItem = 0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)

今天我收到错误 AttributeError: module 'win32com.gen_py.00062FFF-0000-0000-C000-000000000046x0x9x6' has no attribute 'CLSIDToPackageMap'

如果我尝试Excel.Application"或Word.Application",则不会出现错误,并且 Outlook 已安装并在我的系统上运行.上周我遇到了 mail.Bcc 和 mail.HTMLbody 分别更改为 mail.BCC 和 mail.HTMLBody 的问题,但我没有发现更改字符串有帮助.

If I try "Excel.Application" or "Word.Application" then I don't get an error and Outlook is installed and working on my system. Last week I came across the issue where mail.Bcc and mail.HTMLbody changed to mail.BCC and mail.HTMLBody respectively but I've not found that a change in the string has helped.

谁能解释一下可能发生的事情?

Can anyone shed any light on what might be happening?

提前致谢.

推荐答案

您可能需要删除一些旧文件:

You might have to remove some old files:

# If errors are found, do this
# clear contents of C:\Users\<username>\AppData\Local\Temp\gen_py
# that should fix it, to test it type
import win32com.client
app = win32com.client.gencache.EnsureDispatch("Outlook.Application")
app.Visible = True

这个要点还有其他自动删除文件的解决方案.应用需要调整.

This gist also has other solutions that remove the files automatically. Application needs to be adjusted.

1.)

from pathlib import Path
 try:
        xl = win32.gencache.EnsureDispatch('Excel.Application')
    except AttributeError:
        f_loc = r'C:\Users\<username>\AppData\Local\Temp\gen_py'
        for f in Path(f_loc):
            Path.unlink(f)
        Path.rmdir(f_loc)
        xl = win32.gencache.EnsureDispatch('Excel.Application')

2.)

try:
    xl = client.gencache.EnsureDispatch('Excel.Application')
except AttributeError:
    # Corner case dependencies.
    import os
    import re
    import sys
    import shutil
    # Remove cache and try again.
    MODULE_LIST = [m.__name__ for m in sys.modules.values()]
    for module in MODULE_LIST:
        if re.match(r'win32com\.gen_py\..+', module):
            del sys.modules[module]
    shutil.rmtree(os.path.join(os.environ.get('LOCALAPPDATA'), 'Temp', 'gen_py'))
    from win32com import client
    xl = client.gencache.EnsureDispatch('Excel.Application')

这篇关于使用 Python 从 Outlook 发送电子邮件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆