简体   繁体   中英

Email temporary file with Outlook

I want to create a file, email it as an attachment, then delete the file, using JScript.

The problem lies in the fact that deleting the file too soon will prevent Outlook from attaching the file to the email.

I will be creating a MailItem and displaying it with something like the following code. The email will not automatically be sent, but it will be displayed so the user can edit it if necessary.

var outlook = new ActiveXObject("Outlook.Application");
var msg = outlook.CreateItem(0);
msg.Recipients.Add(toAddress);
msg.Subject = subject;
msg.htmlbody = body;
msg.Attachments.Add(attachment);
msg.Display();

My question is, when is it safe to delete the attachment file? I know MailItem has events, where I could maybe delete the attachment file in the AttachmentAdd event. But, AFAIK, there is no way to subscribe to ActiveXObject events in JScript.

My thoughts:

  • I could delete the file immediately, if Attachments.Add is synchronous (or if Display waits until all attachments have been read)
  • I could sleep for x seconds, and then delete the file.
  • Maybe there is a way to subscribe to an event, and I haven't found it.

Details:

This script is, for all intents and purposes, running in wscript.

This is not specific to any Outlook version. It should work with any version.

I will also be attaching several other files to the email.

If you have questions, just ask.

I tried the following code and it worked.

var fso = new ActiveXObject("Scripting.FileSystemObject");

var outlook = new ActiveXObject("Outlook.Application");
var msg = outlook.CreateItem(0);
msg.Subject = "Subject";
msg.Body = "Body";
msg.Attachments.Add(file);
fso.DeleteFile(file); // No problems.
msg.Display();

It isn't mentioned anywhere in the documentation, but Attachments.Add() seems to be synchronous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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