粘贴到文本框中会破坏结束电子邮件构建

时间:2012-11-15 17:13:44

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

我遇到了一个我无法理解的奇怪问题。 我有一个包含多个文本框的表单,其中一个是注释框:

MVC:

<div class="contactUsTextArea">
    Comments or Questions:<br />
    @Html.TextAreaFor(x => x.Comments, new { maxlength = 990 } )
</div>

呈现HTML:

<div class="contactUsTextArea">
    Comments or Questions:
    <br>
    <textarea id="Comments" rows="2" name="Comments" maxlength="990" cols="20"></textarea>
</div>

提交表单后,此代码将运行:

public bool SendEmail(ContactUsModel formSubmission) {
    MailMessage email = new MailMessage();
    SmtpClient smtp = new SmtpClient();
    string upc = formSubmission.ProductUpcCode;
    string comments = formSubmission.Comments;
    string comments_small = formSubmission.Comments;

    if (!string.IsNullOrEmpty(formSubmission.ProductUpcCode) && upc.Length > 14 )
        upc = upc.Substring(0, 13);

    if (!string.IsNullOrEmpty(comments) && comments.Length > 990)
        comments = comments.Substring(0, 989);

    if (!string.IsNullOrEmpty(comments_small) && comments_small.Length > 255)
        comments_small = comments_small.Substring(0, 254);

    string bodyText = "FIRST_NAME:" + formSubmission.FirstName + "\n" +
                        "LAST_NAME:" + formSubmission.LastName + "\n" +
                        "COMPANY:" + formSubmission.CompanyName + "\n" +
                        "ADDRESS:" + formSubmission.StreetAddress + "\n" +
                        "CITY_TOWN:" + formSubmission.City + "\n" +
                        "STATE_PROVINCE:" + formSubmission.Province + "\n" +
                        "ZIP_POSTAL:" + formSubmission.PostalCode + "\n" +
                        "COUNTRY:CAN\n" +
                        "EMAIL:" + formSubmission.Email + "\n" +
                        "PHONE:" + formSubmission.PhoneNumber + "\n" +
                        "UPC:" + upc + "\n" +
                        "DATE_CODE:\n" +
                        "BRAND_PRODUCT:" + formSubmission.ProductName + "\n" +
                        "COMMENTS:" + comments_small + "\n" +
                        "FULL_COMMENTS:" + comments + "\n" +
                        "LANGUAGE:English" + "\n" +
                        "OPTIN:N";
    email.From = new MailAddress(ConfigurationManager.AppSettings["emailSubmission_FROM"]);
    email.To.Add(new MailAddress(ConfigurationManager.AppSettings["emailSubmission_TO"]));
    email.Subject = ConfigurationManager.AppSettings["emailSubmission_SUBJECT"];
    email.IsBodyHtml = false;
    email.Body = bodyText;
    email.BodyEncoding = System.Text.Encoding.UTF8;

    smtp.Send(email);            
    return true;
}

(不要问我为什么需要一个小评论和大评论,客户将是客户)

无论如何,我的问题是当我在评论框中输入评论时,我得到了这个回复:

http://i.imgur.com/zraNy.png

但是当我复制粘贴文本时,我得到了这个回复:

http://i.imgur.com/doWrw.png

为什么会这样?

2 个答案:

答案 0 :(得分:1)

这可能只是一些事情(我能想到) - 两者都与客户有关。

  • 电子邮件客户端无法呈现\n的内容。 虽然对于Windows,\r\n是标准,但许多应用程序都没有正确地尊重“\n”。我会尝试将其用于换行而不仅仅是\n
  • 还有与客户有关的东西,与编码有关(但我对此表示怀疑)

答案 1 :(得分:1)

好吧......所以我只是张贴一张我的问题的照片......然后我将永远躲在洞穴中羞耻。

http://i.imgur.com/tEiKj.png

感谢大家的帮助。