【问题标题】:MVCMailer SendAsync and deleting attachmentsMVCMailer SendAsync 和删除附件
【发布时间】: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);
        }

【问题讨论】:

    标签: asp.net-mvc mailmessage sendasync mvcmailer


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2013-02-12
      • 2012-05-02
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多