简体   繁体   English

Python / Outlook附件:“找不到此文件。验证存在”

[英]Python/Outlook Attachment: “cannot find this file..verify exists”

I wrote the following to use Outlook to send an email, but it is not able to locate the file I want to reference: 我写了以下内容,以使用Outlook发送电子邮件,但找不到我要引用的文件:

import win32com.client as win32
import glob
import os
import datetime

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'xx@ii.ca'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>'# this field is optional

#In case you want to attach a file to the email

newestfile  = max(glob.iglob('*.csv'), key=os.path.getctime))
mail.Attachments.Add(newestfile)

mail.Send()

if i try to print(max(glob.iglob('*.csv'), key=os.path.getctime))) it returns the right path. 如果我尝试print(max(glob.iglob('*.csv'), key=os.path.getctime)))它将返回正确的路径。 Directly writing the path in newestfile also works fine. 直接将路径写入newestfile也可以正常工作。 How come it doesn't attach in its current form? 它为什么不以当前形式附加?

I also tried converting the path to a string, that did not work 我也尝试将路径转换为字符串,但没有用

From the Attachments.Add Method (Outlook) docs, the first argument is: Attachments.Add方法(Outlook)文档中,第一个参数是:

The source of the attachment. 附件的来源。 This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. 这可以是文件(由具有文件名的完整文件系统路径表示)或构成附件的Outlook项目。

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

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