使用Python发送Outlook电子邮件的错误

时间:2018-03-12 14:49:36

标签: python outlook

我正在尝试使用python在outlook中发送电子邮件并遇到错误。我不确定问题的原因。它可能与服务器有关,但错误似乎表明它与脚本有关。电子邮件脚本是:

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

我收到的错误消息说:

“文件”C:\ Users *** \ Desktop \ CORE \ Query.py“,第78行,in     send_notification()

send_notification中的文件“C:\ Users **** \ Desktop \ CORE \ Query.py”,第53行     mail.To =' @ .com',

文件“C:\ Python27 \ lib \ site-packages \ win32com \ client \ dynamic.py”,第565行, setattr     self。 oleobj .Invoke(entry.dispid,0,invoke_type,0,value)“

pywintypes.com_error :( -2147352567,'Exception occurred。',(4096,u'Microsoft Outlook',u'该对象不支持此方法。',None,0,-2147352567),None)“< / p>

enter image description here

如果有人可以提供一些建议,我可以做些什么来使脚本正常工作,我将不胜感激。

谢谢!

2 个答案:

答案 0 :(得分:0)

你可以尝试一下吗?这工作

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

答案 1 :(得分:0)

嗨,下面的内容对我有用,非常简单,您的代码似乎遍地都是。

In [140]: df = pd.DataFrame({"A":["foo", "foo", "foo", "bar"],"B":[0,1,1,1]})
     ...:
     ...:
     ...: df1 = pd.DataFrame({"A":["Panda", "Panda", "Zootopia", "Zootopia"],"B":[0,1,1,1]})

In [141]: df.append(df1)
Out[141]:
          A  B
0       foo  0
1       foo  1
2       foo  1
3       bar  1
0     Panda  0
1     Panda  1
2  Zootopia  1
3  Zootopia  1

请记住,附件中要在Windows系统上使用转义符,即

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