föstudagur, 14. október 2011

CLR error when deleting email attachment file


User was prompted with CLR error when sending PDF email.
The email attachment file could not be deleted after the email was sent.  The file was locked as the mailMessage.Dispose() command was missing.

    //move attachment file to Temp folder
    System.IO.File::Move(p1.fileName(), fileNameforEmail);

    attachementCollection   = mailMessage.get_Attachments();
    attachment              = new System.Net.Mail.Attachment(fileNameforEmail);
    attachementCollection.Add(attachment);

    smtpClient                  = new System.Net.Mail.SmtpClient(smtpServer);
    smtpClient.Send(mailmessage);

    mailMessage.Dispose();

    System.IO.File::Delete(fileNameforEmail);

    CodeAccessPermission::revertAssert();