简体   繁体   English

Python client.Dispatch("outlook.application") 在特定情况下失败(@company 电子邮件)

[英]Python client.Dispatch("outlook.application") fails in specific case (@company emails)

Hi there,你好呀,

Everything is working fine, except one thing I can't send email to clients that have non-standard email (if their email are @gmail, @outlook, etc, it sends the email normally)一切正常,除了一件事我无法向具有非标准电子邮件的客户发送电子邮件(如果他们的电子邮件是@gmail、@outlook 等,它会正常发送电子邮件)

I have a client with the following email client@company.com (not the real email obviously), I can't send that email to him.我有一个客户使用以下电子邮件 client@company.com(显然不是真正的电子邮件),我无法将该电子邮件发送给他。 There goes my method.这是我的方法。 I'd love some help.我很想得到一些帮助。

mail.Send()邮件.发送()

File "", line 2, in Send pywintypes.com_error: (-2147024809, 'The parameter is incorrect.', None, None)发送 pywintypes.com_error 中的文件“”第 2 行:(-2147024809,'参数不正确。',无,无)

def main_send_email(self, to, header, attached_msg, pdf_files=None):
    import pythoncom
    # return super().main_send_email(to, header, attached_msg, pdf_files)
    self.outlook_app = client.Dispatch(
        'outlook.application', pythoncom.CoInitialize())
    s = client.Dispatch("Mapi.Session")
    mail = self.outlook_app.CreateItem(0)

    # set the account
    account = None
    for acc in mail.Session.Accounts:
        if "39" in acc.DisplayName:
            account = acc
    mail._oleobj_.Invoke(*(64209, 0, 8, 0, account))
    # mail.SendUsingAccount = self.outlook_app.Session.Accounts.Item(1)
    # set email sender
    # mail.To = 'silsilinhas@gmail.com'
    mail.To = to
    mail.Subject = header
    mail.HTMLBody = attached_msg
    if pdf_files is not None:
        for pdf in pdf_files:
            mail.Attachments.Add(pdf)
    mail.Send()

First of all, I've noticed the following line of code which is useless and never used:首先,我注意到以下代码行是无用且从未使用过的:

s = client.Dispatch("Mapi.Session")

It seems there is no need to create a new MAPI session in the code if you automate Outlook.如果您自动化 Outlook,似乎不需要在代码中创建新的 MAPI 会话。

Second, the Send method may trigger a security issue when automating Outlook from external applications.其次,当从外部应用程序自动化 Outlook 时, Send方法可能会触发安全问题。 It can be a security prompt or just an exception thrown at runtime.它可以是安全提示,也可以只是运行时抛出的异常。 There are several ways for suppressing such prompts/issues:有几种方法可以抑制此类提示/问题:

  • Use a third-party components for suppressing Outlook security warnings.使用第三方组件来抑制 Outlook 安全警告。 See Security Manager for Microsoft Outlook for more information.有关详细信息,请参阅Microsoft Outlook 安全管理器

  • Use a low-level API which doesn't trigger such issues/prompts instead of OOM.使用不会触发此类问题/提示的低级 API 而不是 OOM。 Or any other third-party wrappers around that API, for example, Redemption.或围绕该 API 的任何其他第三方包装器,例如 Redemption。

  • Develop a COM add-in which has access to the trusted Application object.开发一个可以访问受信任的Application对象的 COM 加载项。 And then communicate from a standalone application with an add-in using standard .Net tools (Remoting).然后使用标准 .Net 工具(远程处理)从独立应用程序与加载项进行通信。

  • Use group policy objects for setting up machines to not throw such issues.使用组策略对象来设置机器不会引发此类问题。

  • Install the latest AV software.安装最新的 AV 软件。

Third, you may try to set recipients for the mail item using the Recipients collection which provides the Resolve method which attempts to resolve a Recipient object against the Address Book.第三,您可以尝试使用Recipients集合设置邮件项目的收件人,该集合提供了Resolve方法,该方法尝试根据通讯簿解析Recipient对象。 Read more about that in the How To: Fill TO,CC and BCC fields in Outlook programmatically article.如何:以编程方式填写 Outlook 中的收件人、抄送和密件抄送字段一文中了解更多信息。

In a program i wrote I have found the error "(-2147024809, 'The parameter is incorrect.', None, None)" whenever I try using mail.Send() before calling mail.Display().在我编写的程序中,每当我在调用 mail.Display() 之前尝试使用 mail.Send() 时,都会发现错误“(-2147024809,'参数不正确',无,无)”。

Seems fairly consistent but can't explain why!似乎相当一致,但无法解释为什么! Hope this helps (sorry if it doesn't!)希望这会有所帮助(对不起,如果没有!)

...
if pdf_files is not None:
       for pdf in pdf_files:
          mail.Attachments.Add(pdf)
mail.Display()  ##this line here  
mail.Send()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM