简体   繁体   中英

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)

I have a client with the following email client@company.com (not the real email obviously), I can't send that email to him. 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)

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.

Second, the Send method may trigger a security issue when automating Outlook from external applications. 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. See Security Manager for Microsoft Outlook for more information.

  • Use a low-level API which doesn't trigger such issues/prompts instead of OOM. Or any other third-party wrappers around that API, for example, Redemption.

  • Develop a COM add-in which has access to the trusted Application object. And then communicate from a standalone application with an add-in using standard .Net tools (Remoting).

  • Use group policy objects for setting up machines to not throw such issues.

  • Install the latest AV software.

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. Read more about that in the How To: Fill TO,CC and BCC fields in Outlook programmatically article.

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().

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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