我无法让MVCMailer在异步发送电子邮件后删除附件。
我不知道如何处理消息以释放附加到消息附件的进程。
按照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);
}发布于 2011-08-12 17:34:15
您可以运行将SmtpClientWrapper从using语句中分离出来,并在清理附件之前手动对其调用dispose。
发布于 2011-08-15 21:58:19
要展示我成功的解决方案是什么:
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);https://stackoverflow.com/questions/7034236
复制相似问题