首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVCMailer SendAsync和删除附件

MVCMailer SendAsync和删除附件
EN

Stack Overflow用户
提问于 2011-08-12 07:49:37
回答 2查看 1.1K关注 0票数 4

我无法让MVCMailer在异步发送电子邮件后删除附件。

我不知道如何处理消息以释放附加到消息附件的进程。

按照here的说明...

代码语言:javascript
复制
    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);
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-12 17:34:15

您可以运行将SmtpClientWrapper从using语句中分离出来,并在清理附件之前手动对其调用dispose。

票数 1
EN

Stack Overflow用户

发布于 2011-08-15 21:58:19

要展示我成功的解决方案是什么:

代码语言:javascript
复制
        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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7034236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档