Auto Send Email Through Outlook via Python Code –

Topbullets.comI wrote an article on How to send email through ASP.NET and it helped a lot of people. Automating such processes always save lot of time and manual intervention. Currently in my project one process takes 45 minutes to run and one needs to look out at screen continuously for any error or if code stops in between. For that I wrote a python code which track the process and send email notification instantly if code stops or runs completely. The article can also be useful for someone who is looking for a python code to know which windows applications are running or open any Windows application using Python.
First of all there are lots of SMTP servers which can be used to send email. Google, Yahoo and Hotmail everyone provided SMTP details. You can easily find python code on the same but in my case I am using Outlook app which is Windows default email application.
The code is written is 3 major segments. 1st segment would raise flag when our process stops or runs successfully, 2nd will check if outlook is running or not and then 3rd will send email notification. I won’t paste 1st segment as it depends on your requirement. You can pass one variable and alter its value based on the outcome and then call any functions. FYI, you can add if/else before line 19 and change ’email body’ accordingly. The below code is well commented. One can easily understand the logic behind and use it. If you have any doubt or suggestion please feel free to comment below.

# -*- coding: utf-8 -*-
Created on Wed Sep 21 15:36:00 2016

@author: Deepesh.Singh

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

# Drafting and sending email notification to senders. You can add other senders' email in the list
def send_notification():
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = ';', 
    mail.Subject = 'Sent through Python'
    mail.body = 'This email alert is auto generated. Please do not respond.'
# Open Outlook.exe. Path may vary according to system config
# Please check the path to .exe file and update below
def open_outlook():
    try:['C:\Program Files\Microsoft Office\Office15\Outlook.exe'])
        os.system("C:\Program Files\Microsoft Office\Office15\Outlook.exe");
        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 == "OUTLOOK.EXE":
        flag = 1
        flag = 0

if (flag == 1):

The article can be helpful for:
1. Send email through Python code
2. How to check if windows application is running or not in Python
3. How to open windows application through Python code
4. Find all running programs in Python


Deepesh Singh


4 thoughts on “Auto Send Email Through Outlook via Python Code –

  1. Thanks for the code. To test this code, can I copy this script into my C drive and run it on command prompt?
    If yes, I tried it already and hitting the following error:

    Traceback (most recent call last):
    File “”, line 1, in
    import win32com.client as win32
    File “C:\Python27\lib\site-packages\win32com\”, line 5, in
    import win32api, sys, os
    ImportError: DLL load failed: %1 is not a valid Win32 application.

    Can you please tell me how to fix this?

  2. Hi Singh,
    sorry to bother you.
    I used your sample code to verify the auto send mail function, but i can’t send out the mail until reopening the outlook again. have you met this problem before?

  3. Hi Singh,
    sorry to bother you.
    I used your sample code to verify the auto send mail function, but i can’t send out the mail until reopening the outlook again. have you met this problem before?
    many thanks.

Please leave your valuable comment.

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s