问题描述
在使用python和win32com api进行简单的邮件自动化时,我遇到了SendUsingAccount的问题.从Windows 7升级到Windows 10时,它会被忽略,或者更糟的是会产生错误.
While working on a simple mail automation with python and win32com api, I had an issue with SendUsingAccount. It was ignored or, worse, generating an error when I upgraded from windows 7 to windows 10.
这是我的原始代码
import win32com.client
o = win32com.client.Dispatch("Outlook.Application")
oacctouse = None
for oacc in o.Session.Accounts:
if oacc.SmtpAddress == "[email protected]":
oacctouse = oacc
break
Msg = o.CreateItem(0)
if oacctouse:
Msg.SendUsingAccount = oacctouse
if to:
Msg.To = ";".join(to)
if cc:
Msg.CC = ";".join(cc)
if bcc:
Msg.BCC = ";".join(bcc)
Msg.HTMLBody = ""
Msg.Send()
导致以下错误:追溯(最近一次通话): 文件"C:\ Program Files(x86)\ JetBrains \ PyCharm 5.0.3 \ helpers \ pydev \ pydevd_exec.py",第3行,在Exec中 exec exp在global_vars,local_vars中 文件",第1行,在 setattr 中的文件"C:\ Python27 \ lib \ site-packages \ win32com \ client \ dynamic.py",行560 self. oleobj .Invoke(entry.dispid,0,invoke_type,0,value)com_error:(-2147417851,'\ x83T \ x81 [\ x83o \ x81 [\ x82 \ xc9 \ x82 \ xe6 \ x82 \ xc1 \ x82 \ xc4 \ x97 \ xe1 \ x8aO \ x82 \ xaa \ x95 \ x95 \ xd4 \ x82 \ xb3 \ x82 \ xea \ x82 \ xdc \ x82 \ xb5 \ x82 \ xbd \ x81B',无,无)
Resulting in the following error:Traceback (most recent call last): File "C:\Program Files (x86)\JetBrains\PyCharm 5.0.3\helpers\pydev\pydevd_exec.py", line 3, in Exec exec exp in global_vars, local_vars File "", line 1, in File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 560, in setattr self.oleobj.Invoke(entry.dispid, 0, invoke_type, 0, value)com_error: (-2147417851, '\x83T\x81[\x83o\x81[\x82\xc9\x82\xe6\x82\xc1\x82\xc4\x97\xe1\x8aO\x82\xaa\x95\xd4\x82\xb3\x82\xea\x82\xdc\x82\xb5\x82\xbd\x81B', None, None)
我的系统是日语的.
我将在下面回答我的问题.
I will answer my issue below.
推荐答案
因此,我偶然在此线程位于最底层(大部分用于VBA,但最后一篇文章解决了python问题).
So, I found the solution to my problem by chance on this thread at the very bottom (most of it is for VBA but the last post solved the python issue).
这是工作代码
import win32com.client
o = win32com.client.Dispatch("Outlook.Application")
oacctouse = None
for oacc in o.Session.Accounts:
if oacc.SmtpAddress == "[email protected]":
oacctouse = oacc
break
Msg = o.CreateItem(0)
if oacctouse:
Msg._oleobj_.Invoke(*(64209, 0, 8, 0, oacctouse)) # Msg.SendUsingAccount = oacctouse
if to:
Msg.To = ";".join(to)
if cc:
Msg.CC = ";".join(cc)
if bcc:
Msg.BCC = ";".join(bcc)
Msg.HTMLBody = ""
Msg.Send()
这篇关于python win32com Outlook 2013 SendUsingAccount返回异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!