0 votes
by (8.4k points)
edited

When I send mail using SMTP, I do it like this:

MailMessage msg = new MailMessage();
msg.Attachments.Add(new Attachment(PathToFileOnDisk, NameOfFile));
// Continue to populate msg
// Finally send msg using SMTP

My question is about the Attachment constructor I pass in the path to a file. What is the Attachment object doing then? Reading the content of the file and storing it in the object? If that is the case, can I simply remove the file from disk immediately after creating the Attachment object? Even before sending the message?

I actually tried this once and it looks like it's working. I would like to get an confirmation if that is the case.

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (18.0k points)
edited
 
Best answer

You are right. When creating an attachment, content of the file is loaded into a memory object. You can remove the file from disk just after the Attachment constructor has finished.

If you would like to restore the file later, you can always save the attachment to disk like:

msg.Attachments[0].Save(PathToFileOnDisk);

Just for the information: If you would like to save memory and you don’t want to load the attachment content, you can do it by setting the MimeOptions.DoNotPreloadAttachments option on the Attachemnt object. In this case you must not remove the source file.

Example:

   MailMessage msg = new MailMessage();
   Attachment att = new Attachment();
   att.Options = MimeOptions.DoNotPreloadAttachments;
   att.SetContentFromFile(PathToFileOnDisk);
   msg.Attachments.Add(att);
...