2011-08-11 55 views
4

我無法在異步發送電子郵件後讓MVCMailer刪除附件。MVCMailer SendAsync並刪除附件

我無法弄清楚如何處理消息以釋放附加到消息附件的進程。

繼說明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語句,並呼籲只有你清理附件之前手動配置就可以了。

+0

我加入我自己的答案只是爲了證明我寫的,但因爲它是基於這個建議,我將標誌着這是正確的答案。 –

1

要展示一下我的成功的解決方案是:

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