0

I have MVCMailer installed at my app. But When i sent the mail with russian charset body i get something like this:

Ïðèâåòñòâóåì Âàñ íà ñàéòå

Good looking, isnt it? :)

For detailed info, can show IUserMailer.cs:

namespace photostorage.Mailers
{ 
    public interface IUserMailer
    {
        MailMessage Welcome(string userMail, string login, string password);
        MailMessage PasswordReset();
    }
}

And UserMailer.cs:

       public virtual MailMessage Welcome(string userMail, string login, string password)
        {
            var mailMessage = new MailMessage{Subject = "Добро пожаловать на FotoStorage.org"};

            mailMessage.To.Add(userMail);
            ViewBag.login = login;
            ViewBag.password = password;
            PopulateBody(mailMessage, viewName: "Welcome");

            return mailMessage;
        }

_Layout.cshtml:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
        @RenderBody()
    </body>
</html>

And Welcome.cshtml

<div>
    <div><h1>Приветствуем Вас на сайте ......</h1></div>
    <div>
        <p>
            Ваш логин: @ViewBag.login
            Ваш пароль: @ViewBag.password
        </p>
        <p>
            Постоянная ссылка на Ваши фото - .......
        </p>
    </div>
</div>

Thanks a lot! :)

Evgeniy Labunskiy
  • 2,012
  • 3
  • 27
  • 45

4 Answers4

6

First add the following:

  x.BodyEncoding = Encoding.UTF8;
  x.SubjectEncoding = Encoding.UTF8;

Then add the following override to the class derived from MailerBase:

public override AlternateView PopulateHtmlPart(MailMessage mailMessage, string viewName, string masterName, Dictionary<string, string> linkedResources)
{
    var htmlPart = base.PopulateHtmlPart(mailMessage, viewName, masterName, linkedResources);
    htmlPart.ContentType.CharSet = mailMessage.BodyEncoding.HeaderName;
    return htmlPart;
}

Voila...

Pontus Netler
  • 61
  • 1
  • 1
  • it works as long as your view doesn't include partials :D Error can be reproduced in Outlook but not in Gmail, because Gmail always converts the content to utf8. Outlook relies on ContentType Header, which has to added to every part of the message, when the message is send as a multi-part. – tomalone Nov 19 '17 at 22:13
4

Set MailMessage.BodyEncoding = Encoding.UTF8.
You may also need to set SubjectEncoding.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Make sure that your template is saved as UTF8 (which should be the default for new files).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
-1

Make sure also your .cs file (source code) where you set subject is saved in UTF8.

AlexB
  • 4,167
  • 4
  • 45
  • 117