简体   繁体   English

Python Outlook 恼人的文件附件错误

[英]Python Outlook vexing file attachment error

So, with this code simply trying to test sending an email attachment using outlook with python:因此,使用此代码只是尝试使用 outlook 和 python 测试发送 email 附件:

Error:错误:

Traceback (most recent call last):
  File "c:\python_code.py", line 10, in <module>
    mail.Attachments.Add("<path to file on C:Drive>")
  File "<COMObject <unknown>>", line 2, in Add
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'File name or directory name is not valid.', None, 0, -2147024773), None)

I have tried nearly ten stackoverflow and other solutions which didn't precisely meet this error, but were close.我已经尝试了近十个 stackoverflow 和其他解决方案,这些解决方案并没有完全满足这个错误,但很接近。 Tried this with different files, filetypes, same error.尝试使用不同的文件、文件类型、相同的错误。 All the files are accessible and otherwise normal.所有文件都可以访问,否则正常。 Pretty much at the end of options at this point.在这一点上,几乎是在选项的末尾。 Ideas?想法?

Below is the code I am using:下面是我正在使用的代码:

file = "<full path to my file to be attached>"

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = '<my email address>'
mail.Subject = 'Example File Attached'
mail.Body = 'Please see the attached file as the Example'
mail.Attachments.Add(file)
mail.Send()

It turns out, putting an 'r' before the filepath, solved the problem it appears.事实证明,在文件路径前加上一个“r”,解决了它出现的问题。 Tried that a few times before posting and it didn't work, now, for no know reason, it works.发帖前试了好几次都不行,现在不知什么原因,可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM