Opening Outlook Using Scripts

Hello everyone,

When I try to run the below code, I receive the error:
An error occurred: (-2146959355, ‘Server execution failed’, None, None)

Can anyone help point me in the right direction?

import win32com.client

def create_outlook_email(recipient, subject, body):
outlook = win32com.client.Dispatch(“Outlook.Application”)
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.Body = body
mail.Display() # Opens the email window
except Exception as e:
print(f"An error occurred: {e}")

if name == “main”:
recipient = input("Recipient email address: ")
subject = input("Email subject: ")
body = input("Email body: ")
create_outlook_email(recipient, subject, body)

fyi …


By the way,

if name == “main ”:

should be:

if __name__ == '__main__':

And the code afterwards should be indented.

If this is just a one page test script where you will not be importing it to another module, then this line is not necessary.

1 Like

It is use the dunders (i think), but because its not using preformatted text the markup means bold text.