2012-10-03 84 views
1

我試圖用Python啓動一些Outlook 2007自動化。我從Steve Townsend得到了這個非常好的腳本(下圖):Send Outlook Email Via Python?試圖從Outlook 2007使用Python發送郵件(win32com.client)

但是我在開始使用時遇到了困難。

import win32com.client 

def send_mail_via_com(text, subject, recipient, profilename="Outlook2007"): 
    s = win32com.client.Dispatch("Mapi.Session") 
    o = win32com.client.Dispatch("Outlook.Application") 
    s.Logon(profilename) 

    Msg = o.CreateItem(0) 
    Msg.To = recipient 

    Msg.CC = "moreaddresses here" 
    Msg.BCC = "address" 

    Msg.Subject = subject 
    Msg.Body = text 

    #attachment1 = "Path to attachment no. 1" 
    #attachment2 = "Path to attachment no. 2" 
    #Msg.Attachments.Add(attachment1) 
    #Msg.Attachments.Add(attachment2) 

    Msg.Send() 


send_mail_via_com("test text","test subject", "[email protected]","Outlook2007") 

,但我得到了以下錯誤:

Traceback (most recent call last): 
    File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 83, in _ 
GetGoodDispatch 
    IDispatch = pythoncom.connect(IDispatch) 
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None) 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\PROJECTS\Python\send_mail_test.py", line 25, in <module> 
    send_mail_via_com("test text","test subject", "[email protected]","Outloo 
k2007") 
    File "C:\PROJECTS\Python\send_mail_test.py", line 4, in send_mail_via_com 
    s = win32com.client.Dispatch("Mapi.Session") 
    File "C:\Python32\lib\site-packages\win32com\client\__init__.py", line 95, in 
Dispatch 
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,c 
lsctx) 
    File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 108, in 
_GetGoodDispatchAndUserName 
    return (_GetGoodDispatch(IDispatch, clsctx), userName) 
    File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 85, in _ 
GetGoodDispatch 
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.II 
D_IDispatch) 
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None) 

這也可能是一些愚蠢的,我已經錯過了。

它的Python 3.2和PyWin32已安裝

非常感謝

回答

11

沒關係......

import win32com.client 
olMailItem = 0x0 
obj = win32com.client.Dispatch("Outlook.Application") 
newMail = obj.CreateItem(olMailItem) 
newMail.Subject = "I AM SUBJECT!!" 
newMail.Body = "I AM IN THE BODY\nSO AM I!!!" 
newMail.To = "[email protected]" 
#newMail.CC = "moreaddresses here" 
#newMail.BCC = "address" 
#attachment1 = "Path to attachment no. 1" 
#attachment2 = "Path to attachment no. 2" 
#newMail.Attachments.Add(attachment1) 
#newMail.Attachments.Add(attachment2) 
#newMail.display() 
newMail.Send() 

這部作品的Python 3.2.3安裝PyWin32。如果你想玩這個,我已經註釋了一些線。

[2017年編輯 - 添加HTML電子郵件支持(如果它的方便,任何人)

import win32com.client 

#some constants (from http://msdn.microsoft.com/en-us/library/office/aa219371%28v=office.11%29.aspx) 
olFormatHTML = 2 
olFormatPlain = 1 
olFormatRichText = 3 
olFormatUnspecified = 0 
olMailItem = 0x0 


obj = win32com.client.Dispatch("Outlook.Application") 
newMail = obj.CreateItem(olMailItem) 
newMail.Subject = "I AM SUBJECT!!" 
newMail.BodyFormat = olFormatHTML #or olFormatRichText or olFormatPlain 
newMail.HTMLBody = "<h1>I am a title</h1><p>I am a paragraph</p>" 
newMail.To = "[email protected]; [email protected]" 

# carbon copies and attachments (optional) 

#newMail.CC = "moreaddresses here" 
#newMail.BCC = "address" 
#attachment1 = "Path to attachment no. 1" 
#attachment2 = "Path to attachment no. 2" 
#newMail.Attachments.Add(attachment1) 
#newMail.Attachments.Add(attachment2) 

# open up in a new window and allow review before send 
newMail.display() 

# or just use this instead of .display() if you want to send immediately 
#newMail.Send() 
1

上面的代碼工作在Python 2.7版爲好。

S = win32com.client.Dispatch( 「Mapi.Session」) 回溯(最近通話最後一個): 在原崗位的錯誤是造成 文件 「」,行1,在 文件「c:\ Python27 \ lib \ site-packages \ win32com \ client__init __。py」中,第95行,在調度 dispatch,userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx) 文件「c:\ PythonGetDispatchAndUserName return(_GetGoodDispatch(IDispatch,clsctx),userN()中的Python27 \ lib \ site-packages \ win32com \ client \ dynamic.py「 AME) 文件 「C:\ Python27 \ lib中\站點包\ win32com \客戶\ dynamic.py」,線85,在_GetGoodDispatch 的IDispatch = pythoncom.CoCreateInstance(IDispatch的,無,CLSCTX,pythoncom.IID_IDispatch)

它用於在Outlook 2003中工作。我想2007年根本不需要MAPI和登錄。

相關問題