Sending an e-mail with a huge attachment

0 votes
asked Feb 15, 2013 by Rebex KB (8,210 points)
edited Feb 15, 2013

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
answered Feb 15, 2013 by Jan Sotola (16,540 points)
edited Feb 15, 2013
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:


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.


   MailMessage msg = new MailMessage();
   Attachment att = new Attachment();
   att.Options = MimeOptions.DoNotPreloadAttachments;