MVCMailer SendAsync 和删除附件
Posted
技术标签:
【中文标题】MVCMailer SendAsync 和删除附件【英文标题】:MVCMailer SendAsync and deleting attachments 【发布时间】:2011-10-25 10:36:51 【问题描述】:异步发送电子邮件后,我无法让 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);
【问题讨论】:
【参考方案1】:您可以将 SmtpClientWrapper 从 using 语句中中断,并在清理附件之前手动调用 dispose。
【讨论】:
我添加自己的答案只是为了展示我写的内容,但由于它基于此建议,我将其标记为正确答案。【参考方案2】:展示我成功的解决方案是什么:
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);
【讨论】:
以上是关于MVCMailer SendAsync 和删除附件的主要内容,如果未能解决你的问题,请参考以下文章
TPL Dataflow,Post() 和 SendAsync() 之间的功能区别是啥?
如何从 Httpclient.SendAsync 调用中获取和打印响应
从 HttpClient SendAsync 请求获取响应时出现无法解释的超时和延迟