[英]Trying to send a mail from Outlook 2007 using Python (win32com.client)
I'm attempting to start some Outlook 2007 automation with Python. 我正在尝试使用Python启动一些Outlook 2007自动化。 I got this great script (below) from Steve Townsend on this thread: Send Outlook Email Via Python?
我在以下线程上从Steve Townsend得到了一个很棒的脚本(下): 通过Python发送Outlook电子邮件吗?
but I'm having trouble getting started with this. 但我在开始时遇到了麻烦。
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", "removed@security.obv","Outlook2007")
But I get the following Errors: 但是我收到以下错误:
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", "removed@security.obv","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)
It's probably something silly that I've missed. 我可能错过了一些愚蠢的事情。
It's Python 3.2 and PyWin32 has been installed 它是Python 3.2,并且已安装PyWin32
Many Thanks 非常感谢
never mind... 没关系...
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 = "who_to_send_to@example.com"
#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()
This works on Python 3.2.3 with PyWin32 installed. 这适用于安装了PyWin32的Python 3.2.3。 I have commented out some lines for if you want to play with this.
我已经注释了一些行,如果你想玩这个。
[2017 EDIT] - adding HTML email support (in case it's handy for anyone) [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 = "who_to_send_to@example.com; anotherrecipient@email.fake"
# 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()
the code above works on Python 2.7 as well. 上面的代码也适用于Python 2.7。 the error in the original post is caused by:
原始帖子中的错误是由以下原因引起的:
s = win32com.client.Dispatch("Mapi.Session") Traceback (most recent call last): File "", line 1, in File "c:\\Python27\\lib\\site-packages\\win32com\\client__init__.py", line 95, in Dispatch dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx) File "c:\\Python27\\lib\\site-packages\\win32com\\client\\dynamic.py", line 108, in _GetGoodDispatchAndUserName return (_GetGoodDispatch(IDispatch, clsctx), userName) File "c:\\Python27\\lib\\site-packages\\win32com\\client\\dynamic.py", line 85, in _GetGoodDispatch IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
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:\\ Python27 \\ lib \\ site-packages \\ win32com \\ client \\ dynamic.py”,第108行,在_GetGoodDispatchAndUserName中返回(_GetGoodDispatch( IDispatch,clsctx),用户名)文件“ c:\\ Python27 \\ lib \\ site-packages \\ win32com \\ client \\ dynamic.py”,第85行,在_GetGoodDispatch中IDispatch = pythoncom.CoCreateInstance(IDispatch,None,clsctx,pythoncom.IID_IDispatch)
it used to work in Outlook 2003. I suppose 2007 simply does not need the MAPI and logon. 它曾经在Outlook 2003中工作。我想2007根本不需要MAPI和登录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.