4

I am attempting to use python to send an email in outlook and am encountering an error. I am not sure the reason for the problem. It may be with the server but the error seems to indicate it is with the script. The email script is:

import win32com.client as win32
import psutil
import os
import subprocess

def send_notification():
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = '[email protected]', 
    mail.Subject = 'Sent through Python'
    mail.body = 'This email alert is auto generated. Please do not respond.'
    mail.send

# Open Outlook.exe. Path may vary according to system config
# Please check the path to .exe file and update below

def open_outlook():
    try:
        subprocess.call(['C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe'])
        os.system("C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe");
    except:
        print("Outlook didn't open successfully")

# Checking if outlook is already opened. If not, open Outlook.exe and send email
for item in psutil.pids():
    p = psutil.Process(item)
    if p.name() == "OUTLOOK.EXE":
        flag = 1
        break
    else:
        flag = 0

if (flag == 1):
    send_notification()
else:
    open_outlook()
    send_notification()

The error message I am getting says:

"File "C:\Users***\Desktop\CORE\Query.py", line 78, in send_notification()

File "C:\Users****\Desktop\CORE\Query.py", line 53, in send_notification mail.To = '@.com',

File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 565, in setattr self.oleobj.Invoke(entry.dispid, 0, invoke_type, 0, value)"

pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'The object does not support this method.', None, 0, -2147352567), None)"

enter image description here

If anyone can provide some advice on what I can do to get the script working, I would appreciate it.

Thanks!

4
  • 1
    Can't be sure this is the cause of the issue so I won't answer, but I believe MailItem.To takes a list of strings and not a single string. Try making it mail.To = ['[email protected]']. Also you have a comma after that line that probably isn't intended to be there. Commented Mar 12, 2018 at 14:57
  • as @GarrettGutierrez said, the comma might be the error, writting just mail.To = '[email protected]' works normally. go check this question Commented Mar 12, 2018 at 15:36
  • Yes, it was the comma. Thanks for the assistance! Commented Mar 13, 2018 at 13:26
  • 1
    Send is a method: mail.send() Commented May 1, 2018 at 20:30

2 Answers 2

2

Hi So the below works for me and is very simple, your code seems a bit all over the place.

import win32com.client

inbox = win32com.client.gencache.EnsureDispatch("Outlook.Application").GetNamespace("MAPI")
print(dir(inbox))
inbox = win32com.client.Dispatch("Outlook.Application")
print(dir(inbox))

mail = inbox.CreateItem(0x0)
mail.To = "[email protected]"
mail.CC = "[email protected]"
mail.Subject = "Send TEST"
mail.Body = "This is the body"
mail.Attachments.Add(u"path to attachment")
mail.Send()

remember that in the attachment to use escape characters on windows systems i.e.

c:\users\me  should be  c:\\users\\me
Sign up to request clarification or add additional context in comments.

1 Comment

great! It works for me.
1

can you try this? this works

import win32com.client as win32
import psutil
import os
import subprocess

def send_notification():
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = '[email protected]'
    mail.Subject = 'Sent through Python'
    mail.body = 'This email alert is auto generated. Please do not respond.'
    mail.Send()


# Open Outlook.exe. Path may vary according to system config
# Please check the path to .exe file and update below

def open_outlook():
    try:
        subprocess.call(['C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe'])
        os.system("C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe");
    except:
        print("Outlook didn't open successfully")

# Checking if outlook is already opened. If not, open Outlook.exe and send email
for item in psutil.pids():
    p = psutil.Process(item)
    if p.name() == "OUTLOOK.EXE":
        flag = 1
        break
    else:
        flag = 0

if (flag == 1):
    send_notification()
else:
    open_outlook()
    send_notification()

2 Comments

Could you please suggest how to send the mail to multiple emails via this approach, it is working fine for the single recipient. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.