smsohan / MvcMailer

A Mailer for ASP.Net MVC that forms the Email Body using MVC Views (Razor etc.) following Ruby on Rails ActionMailer style
MIT License
585 stars 176 forks source link

Logging issues #71

Open Eonasdan opened 11 years ago

Eonasdan commented 11 years ago

I want to log the messages that are sent out. I was doing this:

public virtual MvcMailMessage PasswordReset(string emailTo, string passwordToken)
    {
        ViewBag.passwordToken = passwordToken;

        var mailer = Populate(x =>
        {
            x.Subject = "Password Reset";
            x.ViewName = "PasswordReset";
            x.To.Add(emailTo);
        });
        GeneralHelpers.LogEmail(mailer.Subject, mailer.Body, mailer.To.ToString()); //custom function to save the details to a db
        return mailer;
    }

but I usually get Value does not fall within the expected range. at the var mailer line

If I change me code to this:

public virtual MvcMailMessage PasswordReset(string emailTo, string passwordToken)
    {
        ViewBag.passwordToken = passwordToken;

       return Populate(x =>
        {
            x.Subject = "Password Reset";
            x.ViewName = "PasswordReset";
            x.To.Add(emailTo);
        });
    }

It works, but I don't get any logging.

smsohan commented 11 years ago

Please include a stack trace so we can dig deeper into this.


Sent from my iPhone Sohan SM

On 2012-11-19, at 6:07 AM, Eonasdan notifications@github.com wrote:

I want to log the messages that are sent out. I was doing this:

public virtual MvcMailMessage PasswordReset(string emailTo, string passwordToken) { ViewBag.passwordToken = passwordToken;

    var mailer = Populate(x =>
    {
        x.Subject = "Password Reset";
        x.ViewName = "PasswordReset";
        x.To.Add(emailTo);
    });
    GeneralHelpers.LogEmail(mailer.Subject, mailer.Body,

mailer.To.ToString()); return mailer; }

but I usually get Value does not fall within the expected range. at the var mailer line

If I change me code to this:

public virtual MvcMailMessage PasswordReset(string emailTo, string passwordToken) { ViewBag.passwordToken = passwordToken;

   return mailer = Populate(x =>
    {
        x.Subject = "Password Reset";
        x.ViewName = "PasswordReset";
        x.To.Add(emailTo);
    });
}

It works, but I don't get any logging.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/71.

Eonasdan commented 11 years ago

In the controller I'm doing this:

UserMailer.PasswordReset(userEmail, token).Send();

UserMailer:

public interface IUserMailer
{
    [...]
     MvcMailMessage PasswordReset(string emailTo, string passwordToken);
}

Stack:

    System.ArgumentException occurred
    HResult=-2147024809
   Message=Value does not fall within the expected range.
   Source=mscorlib
   StackTrace:
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
       at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
       at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name)
       at System.Web.Hosting.IIS7WorkerRequest.GetRemoteAddress()
       at System.Web.HttpWorkerRequest.IsLocal()
       at System.Web.HttpRequestWrapper.get_IsLocal()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.StartPage.RunPage()
       at System.Web.WebPages.StartPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at Mvc.Mailer.StringResult.ExecuteResult(ControllerContext context)
       at Mvc.Mailer.StringResult.ExecuteResult(ControllerContext context, String mailerName)
       at Mvc.Mailer.MailerBase.EmailBody(String viewName, String masterName)
       at Mvc.Mailer.MailerBase.PopulateHtmlBody(MailMessage mailMessage, String viewName, String masterName)
       at Mvc.Mailer.MailerBase.PopulateBody(MailMessage mailMessage, String viewName, String masterName, Dictionary`2 linkedResources)
       at Mvc.Mailer.MailerBase.Populate(Action`1 action)
       at projectMgt.Mailers.UserMailer.PasswordReset(String emailTo, String passwordToken) in Mailers\UserMailer.cs:line 96
    InnerException: 

Elmah shows the stack as:

        System.ArgumentException occurred
    HResult=-2147024809
   Message=Value does not fall within the expected range.
   Source=mscorlib
   StackTrace:
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
       at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
       at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name)
       at System.Web.Hosting.IIS7WorkerRequest.GetRemoteAddress()
       at System.Web.HttpWorkerRequest.IsLocal()
       at System.Web.HttpRequestWrapper.get_IsLocal()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.StartPage.RunPage()
       at System.Web.WebPages.StartPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at Mvc.Mailer.StringResult.ExecuteResult(ControllerContext context)
       at Mvc.Mailer.StringResult.ExecuteResult(ControllerContext context, String mailerName)
       at Mvc.Mailer.MailerBase.EmailBody(String viewName, String masterName)
       at Mvc.Mailer.MailerBase.PopulateHtmlBody(MailMessage mailMessage, String viewName, String masterName)
       at Mvc.Mailer.MailerBase.PopulateBody(MailMessage mailMessage, String viewName, String masterName, Dictionary`2 linkedResources)
       at Mvc.Mailer.MailerBase.Populate(Action`1 action)
       at projectMgt.Mailers.UserMailer.PasswordReset(String emailTo, String passwordToken) in Mailers\UserMailer.cs:line 96
    InnerException: `enter code here`

Line 96 is where return Populate.... is

I thought I had it fixed as I mentioned above but it seems to be hit and miss. Some times it works and other times it doesn't

smsohan commented 11 years ago

It appears that in the view file there is something that's making the Razor engine to fail rendering. Can you start with a dumb view, may be just some plain text without variables/dynamic contents?


Sent from my iPhone Sohan SM

On 2012-11-19, at 7:47 AM, Eonasdan notifications@github.com wrote:

In the controller I'm doing this:

UserMailer.PasswordReset(userEmail, token).Send();

UserMailer:

public interface IUserMailer { [...] MvcMailMessage PasswordReset(string emailTo, string passwordToken); }

Stack:

System.ArgumentException occurred HResult=-2147024809 Message=Value does not fall within the expected range. Source=mscorlib StackTrace: at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name) at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name) at System.Web.Hosting.IIS7WorkerRequest.GetRemoteAddress() at System.Web.HttpWorkerRequest.IsLocal() at System.Web.HttpRequestWrapper.get_IsLocal() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.StartPage.RunPage() at System.Web.WebPages.StartPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at Mvc.Mailer.StringResult.ExecuteResult(ControllerContext context) at Mvc.Mailer.StringResult.ExecuteResult(ControllerContext context, String mailerName) at Mvc.Mailer.MailerBase.EmailBody(String viewName, String masterName) at Mvc.Mailer.MailerBase.PopulateHtmlBody(MailMessage mailMessage, String viewName, String masterName) at Mvc.Mailer.MailerBase.PopulateBody(MailMessage mailMessage, String viewName, String masterName, Dictionary2 linkedResources) at Mvc.Mailer.MailerBase.Populate(Action1 action) at projectMgt.Mailers.UserMailer.PasswordReset(String emailTo, String passwordToken) in Mailers\UserMailer.cs:line 96 InnerException:

Line 96 is where return Populate.... is

I thought I had it fixed as I mentioned above but it seems to be hit and miss. Some times it works and other times it doesn't

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/71#issuecomment-10515914.

Eonasdan commented 11 years ago

I took everything out of the View I was using to be this:

You (hopefully) have requested a password reset for [..]. 
Please proceed to the <a href="" >password reset page</a> or copy this link into your browser:
smsohan commented 11 years ago

Is this still failing after things are removed from the view?

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Mon, Nov 19, 2012 at 8:49 AM, Eonasdan notifications@github.com wrote:

I took everything out of the View I was using to be this:

You (hopefully) have requested a password reset for [..]. Please proceed to the password reset page or copy this link into your browser:

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/71#issuecomment-10518433.

Eonasdan commented 11 years ago

yup with the same error message

On Mon, Nov 19, 2012 at 10:53 AM, SM Sohan notifications@github.com wrote:

Is this still failing after things are removed from the view?

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Mon, Nov 19, 2012 at 8:49 AM, Eonasdan notifications@github.com wrote:

I took everything out of the View I was using to be this:

You (hopefully) have requested a password reset for [..]. Please proceed to the password reset page or copy this link into your browser:

— Reply to this email directly or view it on GitHub< https://github.com/smsohan/MvcMailer/issues/71#issuecomment-10518433>.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/71#issuecomment-10518581.

Jonathan Peterson "Desiderio Domini" <><

smsohan commented 11 years ago

Can you try the example project to see if this is a problem with something specific to your project or MvcMailer in general? https://github.com/tylermercier/MvcApplication3

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Mon, Nov 19, 2012 at 8:54 AM, Eonasdan notifications@github.com wrote:

yup with the same error message

On Mon, Nov 19, 2012 at 10:53 AM, SM Sohan notifications@github.com wrote:

Is this still failing after things are removed from the view?

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Mon, Nov 19, 2012 at 8:49 AM, Eonasdan notifications@github.com wrote:

I took everything out of the View I was using to be this:

You (hopefully) have requested a password reset for [..]. Please proceed to the password reset page or copy this link into your browser:

— Reply to this email directly or view it on GitHub< https://github.com/smsohan/MvcMailer/issues/71#issuecomment-10518433>.

— Reply to this email directly or view it on GitHub< https://github.com/smsohan/MvcMailer/issues/71#issuecomment-10518581>.

Jonathan Peterson "Desiderio Domini" <><

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/71#issuecomment-10518663.

ddokshin commented 10 years ago

I have the same problem.