Reputation: 59
I wrote a python script that uses win32com.client.Dispatch("Outlook.Application")
to send automated emails through outlook.
If I run the script myself everything works perfectly fine. But if I run it through Window's task scheduler
it doesn't send the emails.
Just to check if I am running the script properly I made the script output a random text file and that works but email doesn't. Why?
Upvotes: 4
Views: 4401
Reputation: 21
My similar issue has been cleared up. I used task scheduler to call a python script (via batch file) that has the pywin32com module. The python code opens excel and calls a macro. It will run fine from python, cmd and the batch file, but wasn't working when ran through task scheduler. It traced back to errors like: "EnsureDispatch disp = win32com.client.Dispatch(prog_id)"
As noted on this thread, I changed the option to "Run only when user is logged on" and it ran successfully!
The only drawback is that I schedule the task for a time that I'm away from the computer. I suppose I just have to not log off and hope that the cpu doesn't go into sleep mode, but that's not really a big deal in this case.
Upvotes: 1
Reputation: 1817
Do you add any attachments to an email before you send it? I had a similar problem but it is working perfectly now. If I had two different functions in my script to send emails (for example one to send an email in the event of an error, and one to send an email if the script ran successfully) I would get an 'operation aborted' error from Outlook. This was because in one of the functions I would add an attachment to the email, however in the other function I wouldn't. Don't ask me why but this would cause an error. To resolve I just had to add a redundant attachment to the email that didn't need an attachment.
def emailComplete():
ol = DispatchEx("Outlook.Application")
Msg = ol.CreateItem(0)
Msg.To = "[email protected]"
Msg.Subject = "foo complete"
Msg.Attachments.Add("C:\Path\to\blank\attachment.txt") # send a blank attachment to stop the 'operation aborted' error
Msg.Send()
ol.Quit()
def emailError():
ol = DispatchEx("Outlook.Application")
Msg = ol.CreateItem(0)
Msg.To = "[email protected]"
Msg.Subject = "foo errored"
Msg.Attachments.Add("C:\path\to\error\file.txt") # send the error file with the email
Msg.Send()
ol.Quit()
It's not the most elegant solution, but it's got mine working!!
Upvotes: 1
Reputation: 138
I had the same issue with an Excel application I built. When executed during my trouble-shooting I would get an Outlook profile dialog box that I couldn't circumvent. Oddly enough, this dialog box wouldn't appear during scheduled execution. I found a way around this and that was to send email using CDO. I understand that you're not using VBA but this could point you to an alternate method of sending an email (like what I had to do): http://www.rondebruin.nl/win/s1/cdo.htm
Note: I was only able to have success when I set the Security Option to "Run only when user is logged on" (I have full admin rights on my machine and am running w/highest privileges). I have a strong -- but unconfirmed -- suspicion that there is some security setting invoked when the Task Scheduler is involved, at least in a corporate environment.
Upvotes: 0