I’ve been asked to add an attachment link to some notification emails (sent via BPM), and since we don’t have the APR module I unfortunately couldn’t just check the “Include shortcut link”.
The email will work (the email gets sent when a user is assigned a Corrective Action), but then an IO exception gets thrown when I try to delete the attachment file. After further testing, it looks like it is because the file is still in use by the SmtpMailer.
Any suggestions on how I can resolve this issue? I was hoping the using(){} would help to do the disposing/filestream closing, but it doesn’t help me here. Not sure if/where I may have gone wrong.
Here’s the portion of the code I’m having the issues with:
// Send Email
using( Ice.Mail.SmtpMailer mailer = new Ice.Mail.SmtpMailer(this.Session))
{
var message = new Ice.Mail.SmtpMail();
message.SetFrom( GetCompanyAddressAction(ttDMRCorAct_Row.Company) );
message.SetTo(EmailTO);
message.SetCC(EmailCC);
message.SetBcc(EmailBCC);
message.SetSubject(EmailSubject);
message.SetBody(EmailBody);
message.IsBodyHtml = true;
//Add Attachments
Dictionary<string, string> attachments = new Dictionary<string, string>();
attachments.Add("AttachmentInfo", attachmentPath);
//Send
mailer.Send(message, attachments);
}
File.Delete(attachmentPath); //file does not get deleted here, IO error is thrown
I was able to get the stream to close using one of the other .send methods, which uses an actual Stream instead of the file path in a string (and then putting that Stream in another using statement). Crisis averted.