3

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

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!

JJMeadows3
  • 81
  • 1
  • 2
  • 3
  • 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 = ['me@mycompany.com']`. Also you have a comma after that line that probably isn't intended to be there. – Garrett Gutierrez Mar 12 '18 at 14:57
  • as @GarrettGutierrez said, the comma might be the error, writting just `mail.To = 'me@mycompany.com'` works normally. go check [this question](https://stackoverflow.com/questions/22681903/send-email-to-multiple-recipients-using-win32com-module-in-python) – Ben.T Mar 12 '18 at 15:36
  • Yes, it was the comma. Thanks for the assistance! – JJMeadows3 Mar 13 '18 at 13:26
  • 1
    Send is a method: mail.send() – Dmitry Streblechenko May 01 '18 at 20:30

2 Answers2

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 = 'forcepointtester1@outlook.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()
pxsubra
  • 11
  • 2
  • Could you please suggest how to send the mail to multiple emails via this approach, it is working fine for the single recipient. Thanks – Chandra Shekhar Nov 25 '21 at 12:50
1

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 = "testTo@test.com"
mail.CC = "testcc@test.com"
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
Dayantat
  • 108
  • 1
  • 6