尝试使用Python从Outlook 2007中发送邮件(win32com.client) [英] Trying to send a mail from Outlook 2007 using Python (win32com.client)

查看:3308
本文介绍了尝试使用Python从Outlook 2007中发送邮件(win32com.client)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图启动一些Outlook 2007中的自动化与Python。我得到了这个伟大的脚本(下)从史蒂夫汤森在此线程:发送Outlook电子邮件通过Python

I'm attempting to start some Outlook 2007 automation with Python. I got this great script (below) from Steve Townsend on this thread: Send Outlook Email Via Python?

但我有麻烦开始使用此。

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.

这是Python的3​​.2和PyWin32已经安装

It's Python 3.2 and PyWin32 has been installed

非常感谢

推荐答案

没关系...

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

安装PyWin32这个工程上的Python 3.2.3。我已经注释掉了,如果你想与此打一些行。

This works on Python 3.2.3 with PyWin32 installed. I have commented out some lines for if you want to play with this.

这篇关于尝试使用Python从Outlook 2007中发送邮件(win32com.client)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆