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
584 stars 178 forks source link

Geting encoding problem only when I use linkedResources #28

Open Medes opened 12 years ago

Medes commented 12 years ago

Hi, I have used your mvcmailer application which is fantastic, in my "contact us" form. the only problem is when I use linkedResources in populateBody method, the email will be unreadable it contains strange characters. when i remove linkedResources in the method I get the email as it is. I need to use dictionary<string, string> as LinkedResources in populateBody method to show our logo. any advice ? thank you. here is the code mailMessage.To.Add("info@blah.com"); ViewData = new ViewDataDictionary(c); mailMessage.BodyEncoding = System.Text.Encoding.UTF8;

        PopulateBody(mailMessage, viewName: "ContactUs", linkedResources: resources);
romias commented 11 years ago

Yeah... all my emails are OK but the ones that uses a embedded image using:

var resources = new Dictionary<string, string>(); resources["logo"] = @"logopath"; PopulateBody(mailMessage, viewName: "Welcome", linkedResources: resources)

Spanish emails with "ñ" or "á" are not displayed correctly.

If I remove the linked resources... it works just fine.

Any one that look into the code and see what can be done? I really want to embed my logo :)

Thanks!

romias commented 11 years ago

I think the issue could be fixed setting encoding on the AlternateView object:

var alternateView = new AlternateView(fooFileName, fooMediaType) { ContentType = { CharSet = Encoding.UTF8.WebName } };

Could you have a try?

momoski commented 11 years ago

Hi Romias,

I had the same issue. I did the following to solve it. I used the following snippet with the encoding line to solve it:

        return Populate(x =>
                            {
                                x.Subject = ViewBag.Subject;
                                x.ViewName = "Welcome";
                                x.BodyEncoding = Encoding.UTF8;
                                x.To.Add(emailAddress);
                            });
romias commented 11 years ago

Hi momoski, Doing this your way, doesn't fix the issue AFAIK... remember the problem appears ONLY when embedding an image. Try this:

var resources = new Dictionary<string, string>();
resources["logo"] = HttpContext.Current.Server.MapPath("YourLogoImagePath");

return Populate(x =>
                            {
                                x.Subject = ViewBag.Subject;
                                x.ViewName = "Welcome";
                                x.BodyEncoding = Encoding.UTF8;
                                x.To.Add(emailAddress);
                                x.LinkedResources = resources;
                            });

In your "Welcome" view place this:

         @Html.InlineImage("logo", "This is my logo")

or directly an IMG tag with the source pointing to "cid:logo"

Thanks!

momoski commented 11 years ago

I do have an image in my mail, however it is an image from a url.

In my masterpage for the mail I got this:

                      <td valign="top" style="border-collapse: collapse;">
                          <table>
                              <tr>
                                  <td>
                                      <img class="image_fix" src="http://mydomain/images/my-logo.png" alt="My company" title="My company" width="x" height="x" style="outline: none; text-decoration: none; -ms-interpolation-mode: bicubic; display: inline;"/>
                                  </td>
                                  <td>
                                      <h1 style="color:#E57D03">MY TAGLINE</h1>
                                  </td>
                              </tr>
                          </table>
                      </td>
                  </tr>
                  @RenderBody()

That image gets rendered without issues and characters appear normal.

I haven't tried embedding the image. Is it not possible for you to just link to the image on a webserver?

erroric commented 11 years ago

Have same problem! After couple of hours I have found solution (just add this override method into your Mailer class and set proper body encoding, ex. x.BodyEncoding = Encoding.UTF8;):

public override AlternateView PopulateHtmlPart(MailMessage mailMessage, string viewName, string masterName, Dictionary<string, string> linkedResources)
        {
            string mime = "text/html";

            if (mailMessage.BodyEncoding != null) 
            { 
                mime +=  "; charset=" + mailMessage.BodyEncoding.WebName;
            }

            var htmlPart = PopulatePart(mailMessage, viewName, mime, masterName);
            if (htmlPart != null)
            {
                PopulateLinkedResources(htmlPart, linkedResources);
            }

            return htmlPart;
        }

I think, same bug exists inside PopulateTextPart also

romias commented 11 years ago

@erroric, Thanks a lot!!

That fixes the issue... Hope this gets into the root code :)

Regards!