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 = 'me@mycompany.com',
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)"
If anyone can provide some advice on what I can do to get the script working, I would appreciate it.
Thanks!