2011-08-11 8 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

あなたはusingステートメントの外にSmtpClientWrapperを破る実行し、クリーンアップ直前に添付ファイルを手動でそれを処分呼び出すことができます。

+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); 
関連する問題