tags:

views:

86

answers:

2

I am writing a simple email helper class that will be called by a Windows service. When I test though the email attachment is not sending with the rest of the email.

mailAttachmentFilePath is an ArrayList (just for clarification) and mail represents MailMessage class.

if (mailAttachmentFilePath.Count > 0)
        {
            foreach (string file in mailAttachmentFilePath)
            {
                Attachment data = new Attachment(file);
                mail.Attachments.Add(data);
                data.Dispose();
            }
        }

I am certain I am missing something but, I don't know what it is...

+5  A: 

Do the data.Dispose() AFTER you send the email :D.

Al Katawazi
+2  A: 

Remove the data.Dispose(). The attachments are being added by reference so when you call dispose it's actually releasing the attached file. You also don't really need the if statement. Try this:

    foreach (string file in mailAttachmentFilePath)
    {
        Attachment data = new Attachment(file);
        mail.Attachments.Add(data);
    }
Chris Pebble