尝试使用Python发送到Outlook联系人组时出错

时间:2018-03-08 20:57:52

标签: python outlook

我需要使用python和outlook向不同的联系人组发送一些附件的电子邮件。我创建的联系人组称为“测试”。它不会将电子邮件发送到“测试”Outlook联系人组。

运行代码时出现错误消息:

追踪(最近一次呼叫最后一次):

文件“”,第11行,in     send_notification(ⅰ)

文件“”,第10行,在send_notification中     mail.send

文件“C:\ Users \ thisguy \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ win32com \ client \ dynamic.py”,第516行, getattr     ret = self。 oleobj .Invoke(retEntry.dispid,0,invoke_type,1)

com_error:(-2147352567,'发生异常。',(4096,'Microsoft Outlook','Outlook无法识别一个或多个名称。',无,0,-2147467259),无)

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




brands = ['PM', 'KP', 'SW', 'GM']

morning = '<p>Good Morning,</p>'                    

# Drafting and sending email notification to senders. You can add other senders' email in the list
def send_notification(i):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = 'Test' # Contact Group I want to send the email to
    mail.Subject = 'Expenses'
    mail.Attachments.Add(Source = 'c:/users/thisguy/desktop/Expenses.docx')
    for f in glob.glob('c:/users/thisguy/desktop/'+ i +' Expense*.xlsx'):
        mail.Attachments.Add(Source = f)
    mail.HTMLBody =  morning          
    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\root\Office16\OUTLOOK.EXE'])
        os.system("C:\Program Files (x86)\Microsoft Office\root\Office16\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):
    for i in brands:
        send_notification(i)
else:
    open_outlook()
    for i in brands:
        send_notification(i)

0 个答案:

没有答案