MVCMailer SendAsync and Removing Attachments

I'm having trouble getting MVCMailer to delete attachments after sending email asynchronously.

I can’t understand what to do to get rid of the message, to free the processes associated with message attachments.

Following the instructions here ....

private IUserMailer userMailer = new UserMailer(); public IUserMailer UserMailer { get { return this.userMailer; } set { this.userMailer = value; } } using (SmtpClientWrapper client = new SmtpClientWrapper()) { client.SendCompleted += (sender, e) => { if (e.Error != null || e.Cancelled) { // Handle Error } //Use e.UserState //?? How can I use the userstate?? There are no // instructions?? // Delete the saved attachments now. // This will not work since the mailmessage process // is still attached. Parallel.ForEach(imageList, image => { if (System.IO.File.Exists(image)) { System.IO.File.Delete(image); } }); }; // SendAsync() extension method: using Mvc.Mailer // farm is my model imageList is a list of file locations for the // uploaded attachments UserMailer.Submission(farm, imageList).SendAsync("user state object", client); } 
+4
source share
2 answers

You can start the SmtpClientWrapper gap from the using statement and output the utility to it manually before cleaning the attachment.

+1
source

To show what my successful solution was:

  MailMessage message = UserMailer.Submission(farm, imageList); SmtpClientWrapper client = new SmtpClientWrapper(); client.SendCompleted += (sender, e) => { if (e.Error != null || e.Cancelled) { // Handle Error } if (message != null) { message.Attachments.Dispose(); message.Dispose(); // Delete the saved attachments now Parallel.ForEach(imageList, image => { if (System.IO.File.Exists(image)) { System.IO.File.Delete(image); } }); } client.Dispose(); }; // SendAsync() extension method: using Mvc.Mailer message.SendAsync("farm message", client); 
+1
source

All Articles