c# - asp.net mvc 4 来自站点编码的字母

标签 c# asp.net-mvc asp.net-mvc-4

我的 mvc 网站上有反馈表,我需要将此表发送到电子邮件。
我想测试我的表单,因此我将 MailMessage 对象作为 *.eml 文件保存到磁盘

[HttpGet]
public ActionResult Contacts()
{
    FeedbackForm temp = new FeedbackForm();
    temp.Message = @Resources.Global.Contacts_Form_Message_Field;
    return View(temp);
}

[HttpPost]
public ActionResult Contacts(FeedbackForm Model)
{    
    MailMessage msg = new MailMessage();
    msg.BodyEncoding = Encoding.UTF8;            
    msg.From = new MailAddress(Model.Email,
                              @Resources.Global.Contacts_Form_Email_Title);
    msg.To.Add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3246534b5c531f535c5b4653725f535b5e1c4047" rel="noreferrer noopener nofollow">[email protected]</a>");

    string message = @Resources.Global.Contacts_Form_Name_Field + ": "
                     + Model.Name + "\n"
                     + "Email: " + Model.Email + "\n"
                     + @Resources.Global.Contacts_Form_Phone_Field + ": "
                     + Model.Phone + "\n\n"
                     + Model.Message;
    msg.Body = message;
    msg.IsBodyHtml = false;          

    SmtpClient client = new SmtpClient("mysmtphost");
    client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
    client.PickupDirectoryLocation = @"C:\test";

    try
    {
        client.Send(msg);
    }
    catch (Exception ex)
    {
    }

    FeedbackForm tempForm = new FeedbackForm();
    tempForm.Message = @Resources.Global.Contacts_Form_Message_Field;

    return View(tempForm);
}

我得到:

 X-Sender: "Letter from site" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64171717171717172409050d084a1611" rel="noreferrer noopener nofollow">[email protected]</a>>  
 X-Receiver: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe8a9f87909fd39f90978a9fbe939f9792d08c8b" rel="noreferrer noopener nofollow">[email protected]</a>   
 MIME-Version: 1.0   
 From: "Letter from site" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3043434343434343705d51595c1e4245" rel="noreferrer noopener nofollow">[email protected]</a>>  
 To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cf8edf5e2eda1ede2e5f8edcce1ede5e0a2fef9" rel="noreferrer noopener nofollow">[email protected]</a>  
 Date: 21 May 2013 16:57:55 +0400  
 Content-Type: text/plain; charset=utf-8   
 Content-Transfer-Encoding: base64   

 TmFtZTogdGVzdCB0ZXNzc3NzdApFbWFpbDogc3Nzc3Nzc0BtYWlsLnJ1ClBob25lOiA4
 OTIwODcxMzA2MQoKW2F1XSBDaG9pY2VzIChjb25maWRlIGluIG1lKSAtIGhhcnZleSB4
 IG1pa2UgW2F1XSBDaG9pY2VzIChjb25maWRlIGluIG1lKSAtIGhhcnZleSB4IG1pa2Vb
 YXVdIENob2ljZXMgKGNvbmZpZGUgaW4gbWUpIC0gaGFydmV5IHggbWlrZQ==

为什么信的正文看起来是这样的?

我将从网站用英语和俄语发送信件,并设置 msg.BodyEncoding = Encoding.UTF8;

我应该怎么做才能不出现编码问题?

最佳答案

您看到的是一条用 Base-64 编码的消息。您可以从电子邮件标题中看到这一点:

Content-Transfer-Encoding: base64

据推测,您正在纯文本编辑器中打开邮件,因此电子邮件内容不会被解码。

如果您使用 Base64 Decoder 解码消息(任何电子邮件客户端都会自动执行此操作),您会得到以下结果:

Name: test tessssst Email: [email protected] Phone: 89208713061

[au] Choices (confide in me) - harvey x mike [au] Choices (confide in me) - harvey x mike[au] Choices (confide in me) - harvey x mike

关于c# - asp.net mvc 4 来自站点编码的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16670871/

相关文章:

c# - Visual Studio - Resx 文件默认 'internal' 到 'public'

jquery - ASP.NET MVC4 jquery/javascript 包的使用

javascript - 在 cshtml 中的迭代表上设置 ng-bind-html

jquery - MVC 4 客户端验证不适用于使用 Ajax 加载的表单

c# - 如何获取字符串末尾的数字?

c# - 如何从 .NET 客户端应用程序加载 URL

asp.net-mvc - 我的 ViewBag 无法工作有什么原因吗?

c# - 使用 "very very"大数组

c# - 不允许使用抽象字段。如何强制派生类实例化/初始化字段?

asp.net - 如果/何时使用 "Azure Session State Provider (redis)"不需要使用 User.Identity.Name/IsAuthenticated?