Commits

Scott Anderson  committed b5614ff

Changing the way we dispose of IMailSender to hopefully fix some issues with reusing a MailerBase instance for multiple emails in a row.

  • Participants
  • Parent commits 32e2a1a

Comments (0)

Files changed (3)

File src/ActionMailer.Net.Mvc/EmailResult.cs

         /// <param name="viewName">The view to use when rendering the message body (can be null)</param>
         /// <param name="masterName">The maste rpage to use when rendering the message body (can be null)</param>
         /// <param name="messageEncoding">The encoding to use when rendering a message.</param>
+        /// <param name="trimBody">Whether or not we should trim whitespace from the beginning and end of the message body.</param>
         public EmailResult(IMailInterceptor interceptor, IMailSender sender, MailMessage mail, string viewName, string masterName, Encoding messageEncoding, bool trimBody) {
             if (interceptor == null)
                 throw new ArgumentNullException("interceptor");

File src/ActionMailer.Net.Mvc/MailerBase.cs

     /// The base class that your controller should inherit from if you wish
     /// to send emails through ActionMailer.Net.
     /// </summary>
-    public abstract class MailerBase : ControllerBase, IMailerBase {
+    public abstract class MailerBase : Controller, IMailerBase {
         /// <summary>
         /// A string representation of who this mail should be from.  Could be
         /// your name and email address or just an email address by itself.
             Attachments = new AttachmentCollection();
             MailSender = mailSender ?? new SmtpMailSender();
             MessageEncoding = defaultMessageEncoding ?? Encoding.UTF8;
-            if (HttpContext.Current != null) {
-                HttpContextBase = new HttpContextWrapper(HttpContext.Current);
-            }
         }
 
         /// <summary>
         /// Constructs your mail message ready for delivery.
         /// </summary>
         /// <param name="viewName">The view to use when rendering the message body.</param>
+        /// <param name="model">The model object used while rendering the message body.</param>
         /// <param name="masterName">The master page to use when rendering the message body.</param>
-        /// <param name="model">The model object used while rendering the message body.</param>
+        /// <param name="trimBody">Whether or not we should trim whitespace from the beginning and end of the message body.</param>
         /// <returns>An EmailResult that you can Deliver();</returns>
         public virtual EmailResult Email(string viewName, object model = null, string masterName = null, bool trimBody = true) {
             if (viewName == null)
         /// Nothing to do here, left empty for now.
         /// </summary>
         protected override void ExecuteCore() { }
+
+        /// <summary>
+        /// Dispose of the underlying MailSender when this controller is destroyed.
+        /// </summary>
+        /// <param name="disposing">Whether we are disposing or not.</param>
+        protected override void Dispose(bool disposing) {
+            if (disposing) {
+                if (MailSender != null) {
+                    MailSender.Dispose();
+                    MailSender = null;
+                }
+
+                base.Dispose();
+            }
+        }
     }
 }

File src/ActionMailer.Net/DeliveryHelper.cs

             }
 
             if (async) {
-                // we don't have a using here because the SmtpClient will be disposed
-                // before the async delivery is finished.  Therefore, we will Dispose()
-                // the MailSender instance in the AsyncSendCompleted method.
                 _sender.SendAsync(mail, AsyncSendCompleted);
                 return;
             }
 
         private void AsyncSendCompleted(MailMessage mail) {
             _interceptor.OnMailSent(mail);
-            _sender.Dispose();
         }
     }
 }