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)"
If anyone can provide some advice on what I can do to get the script working, I would appreciate it.
Thanks!
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
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With