【发布时间】:2014-07-14 19:46:00
【问题描述】:
这是我的第一个问题,所以我希望没问题。
我正在尝试使用带有 Outlook 2010 的 win32com 使用 Python 发送带有附件的电子邮件。它会发送电子邮件。但它不发送附件。我的代码基于此处给出的示例。
def email_tamplate(*args):
Format = { 'UNSPECIFIED' : 0, 'PLAIN' : 1, 'HTML' : 2, 'RTF' : 3}
profile = "Outlook"
#session = win32com.client.Dispatch("Mapi.Session")
outlook = win32com.client.Dispatch("Outlook.Application")
#session.Logon(profile)
mainMsg = outlook.CreateItem(0)
mainMsg.To = str(login_entry.get())+"@amazon.com"
mainMsg.Subject = "Sauron personal report request between "+str(start_date.get())+" and "+str(end_date.get())
mainMsg.BodyFormat = Format['RTF']
mainMsg.HTMLBody = body
try:
attachment1= ('C:\\Users\\' + str(login_entry.get()) + '\\My Documents\\status_email.csv')
mainMsg.Attachments.Add(attachment1)
except:
pass
mainMsg.Send()
这是我在不使用 Try/except 时收到的错误消息:
Tkinter 回调异常 回溯(最近一次通话最后): 调用中的文件“C:\Python33\lib\tkinter__init__.py”,第 1475 行 返回 self.func(*args) 文件“C:\Users\alvaros\Dev\Sauron\v2\Sauron v2.2.1.py”,第 478 行,在 status_email 电子邮件模板() 文件“C:\Users\alvaros\Dev\Sauron\v2\Sauron v2.2.1.py”,第 424 行,在 email_tamplate mainMsg.Attachments.Add(附件1) 文件“>”,第 3 行,在添加中 pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "You don't have appropriate permission to perform this operation.", None, 0, -2147024891), None)
除了不发送附件外,一切正常。附件的路径是正确的,文件在那里。
谢谢。
【问题讨论】:
-
删除 try/except 并查看是否出现错误。如果是,请在问题中发布例外情况。
-
我刚做了。谢谢。
-
如果您添加一行
print "attachment exists:", os.path.exists(attachment1),它是否打印为True?如果您尝试附加纯文本文件怎么办? -
输入该行时不打印任何内容。附加纯文本文件时也会出现同样的错误。