MVCMailer SendAsync 和删除附件

发布于 2024-11-29 07:55:28 字数 1569 浏览 0 评论 0原文

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

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

按照此处的说明进行操作...

    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);
        }

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

I can't figure out what to do to dispose of the message to free up processes attached to the 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);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无名指的心愿 2024-12-06 07:55:28

您可以在清理附件之前运行将 SmtpClientWrapper 从 using 语句中删除并手动调用 dispose。

You could run break the SmtpClientWrapper out of the using statement and call dispose on it manually just before you clean-up the attachment.

沉睡月亮 2024-12-06 07:55:28

为了展示我的成功解决方案是:

        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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文