开发者

MVCMailer SendAsync and deleting attachments

开发者 https://www.devze.com 2023-03-27 07:06 出处:网络
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 messag

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",
                       开发者_Python百科                                     client);
        }


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


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

精彩评论

暂无评论...
验证码 换一张
取 消