MvcMailer icon indicating copy to clipboard operation
MvcMailer copied to clipboard

Geting encoding problem only when I use linkedResources

Open Medes opened this issue 13 years ago • 7 comments

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("[email protected]"); ViewData = new ViewDataDictionary(c); mailMessage.BodyEncoding = System.Text.Encoding.UTF8;

        PopulateBody(mailMessage, viewName: "ContactUs", linkedResources: resources);

Medes avatar Oct 08 '11 21:10 Medes

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 avatar Sep 29 '12 22:09 romias

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?

romias avatar Sep 30 '12 14:09 romias

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

momoski avatar May 20 '13 09:05 momoski

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!

romias avatar May 20 '13 13:05 romias

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?

momoski avatar May 21 '13 06:05 momoski

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

erroric avatar Jun 06 '13 17:06 erroric

@erroric, Thanks a lot!!

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

Regards!

romias avatar Jun 06 '13 18:06 romias