【问题标题】:win32com not sending attachmentswin32com 不发送附件
【发布时间】: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?如果您尝试附加纯文本文件怎么办?
  • 输入该行时不打印任何内容。附加纯文本文件时也会出现同样的错误。

标签: python win32com


【解决方案1】:

您可能必须以管理员身份运行该程序,或者如果这不起作用,那么[在此处输入链接描述][1]您可能需要使用“扩展 MAPI”来使其工作:

另一方面,该主题讨论了编辑 PST 文件本身的属性或在 Windows 注册表中编辑其属性:

【讨论】:

  • 谢谢。我以管理员身份尝试过,但它给了我另一个错误,即服务器执行失败。我还尝试了您提供的链接中所述的扩展 MAPI。仍然得到错误:(-2147024809, 'The parameter is wrong.', None, None)
【解决方案2】:

这已经解决了。 感谢您的帮助。 这可能是一个缩进问题。因为它基本上是自己解决的。

【讨论】:

    猜你喜欢
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 2019-07-02
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多