HTML电子邮件中的重音字符

时间:2013-09-25 20:50:00

标签: c# html asp.net-mvc razor

我在西班牙语网站(ASP.Net MVC3)中有以下表单元素 (当然是截断的)

<label for="RazonSocial">Raz&oacute;n Social</label>
<input type="text" id="RazonSocial" name="RazonSocial" class="span12"  value=""/>
<label class="radio" for="razon_social_emp">Empresa
    <input type="radio" name="Razon_Social" id="razon_social_emp" value="Empresa" />
</label>
<label class="radio" for="razon_social_pf">Persona F&iacute;sica
    <input type="radio" name="Razon_Social" id="razon_social_pf" value="Persona Física" />
</label>

现在,我正在使用此模型在HTML电子邮件中发送这些值: (当然是截断的)

public class RegistrationEmailModel
{
    public string RazonSocial { get; set; }

    public string Razon_Social { get; set; }
}

这是电子邮件视图: (当然是截断的)

@model MyNamespace.Models.RegistrationEmailModel
<html>
    <head>
        <style type="text/css">
            body { font-family:"Lucida Grande",verdana,arial,helvetica,sans-serif; font-size:11px;}
            .h3 {color:#CC6600; font-weight: bold; font-size: 14px;}
            .header { margin-top: 10px; margin-bottom: 5px; text-align: left; border-bottom: 1px solid #C6600; }
            .p10 { font-size 11px; margin-top: 3px; margin-bottom: 3px;}
            .rt {text-align: right; width: 25%; }
        </style>
    </head>
    <body>
        <table width="660px" border="0" cellpadding="0" cellspacing="0" class="style1">
            <tr>
                <td class="rt">
                    <span class="p10">Razón Social:&nbsp;</span>
                </td>
                <td>
                    <span class="p10"><b>@Model.RazonSocial</b></span>
                </td>
            </tr>
            <tr>
                <td class="rt">
                    <span class="p10">Tipo de Raz&oacute;n Social:&nbsp;</span>
                </td>
                <td>
                    <span class="p10"><b>@RazonSocial</b></span>
                </td>
            </tr>
        </table>
    </body>
</html>

我遇到的问题是,带有重音字符的电子邮件中没有Razon_Social单选按钮的值。 如何将其转换为正确发送电子邮件中的重音字符?

修改 这是RenderPartialViewToString()函数:

public static string RenderPartialViewToString(Controller controller, string viewName, object model)
{
    controller.ViewData.Model = model;
    try
    {
        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }
}

2 个答案:

答案 0 :(得分:2)

尝试将utf-8元标题添加到您的电子邮件模板中。

如果您使用的是HTML5:

<meta charset="utf-8">

或者如果您使用的是HTML 4:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

顺便说一下,使用正确的DOCTYPE字段指定您正在使用的内容。例如,HTML5将是:

<!DOCTYPE html>

哦,顺便说一句,有点偏离主题,但电子邮件中的单选按钮?真?为什么?

答案 1 :(得分:0)

我最终使用了ActionMailer.Net(http://geeksharp.com/2011/01/26/actionmailer-net-email-templates-for-the-mvc-crowd/)并编写了一个OnMailSending()处理程序来替换有问题的字符。我不确定是否有任何其他角色会导致问题,但至少它现在有效。我会继续监控。

我的MailController中的OnMailSending()处理程序:

protected override void OnMailSending(MailSendingContext context)
{
    for (var i = 0; i < context.Mail.AlternateViews.Count; i++)
    {
        var av = context.Mail.AlternateViews[i];
        if (av.ContentType.MediaType == MediaTypeNames.Text.Html)
        {
            av.ContentStream.Position = 0;
            var content = new StreamReader(av.ContentStream).ReadToEnd();
            content = content.Replace("F?sica", "Fí­sica");
            context.Mail.AlternateViews[i] = AlternateView.CreateAlternateViewFromString(content, av.ContentType);
        }
    }            
    base.OnMailSending(context);
}