在smtp消息体中的System.Environment.NewLine将没有任何效果



我有以下在我的asp.net mvc web应用程序,我有一个名为扫描字符串。存储操作执行细节的描述:-

foreach(var c in Assets){
//code goes here
     scan.Description = scan.Description + c.ScanResult + " "" + System.Environment.NewLine;
//code goes here
}

则在此动作方法结束时,我将发送最终扫描。通过电子邮件发送的描述字符串如下:-

using (MailMessage mail = new MailMessage(from, "*****"))
            {
                mail.Subject = "scan report generated";

                mail.IsBodyHtml = true;
            System.Text.StringBuilder mailBody = new System.Text.StringBuilder();
            mailBody.AppendLine("<span style='font-family:Segoe UI'><b>Hi </b><b></b> <br/><br/>");
            mailBody.AppendLine(scan.Description );
            mailBody.AppendLine("<br/><br/><div style='color:#f99406;font-weight:bold'>scanning Management </div> <br/> <div style='color:#f99406;font-weight:bold'>Best Regards</div></span>");
            SmtpClient smtp = new SmtpClient();
            smtp.Host = System.Web.Configuration.WebConfigurationManager.AppSettings["smtpIP"];
            smtp.EnableSsl = true;
            mail.Body = mailBody.ToString();
            //    NetworkCredential networkCredential = new NetworkCredential(from, "BetherealwayS$1");
            smtp.UseDefaultCredentials = false;
            //   smtp.Credentials = networkCredential;
            smtp.Port = Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["smtpPort"]);

            smtp.Send(mail);
        }

但我面临的问题是,电子邮件正文将没有任何System.Environment.NewLine,它将是一个单一的段落?

Environment.NewLine并不意味着"这将神奇地工作在任何地方作为一个新的行"。它只是表示特定操作系统/宿主环境的换行符序列。

在您的情况下,因为您正在发送HTML消息,您需要使用HTML换行-也就是说,<p>...</p><br />取决于您正在谈论的确切类型的新行。

期望Environment.NewLine为这样的场景工作就像期望它为例如HTTP头(正确的是总是 CRLF,无论环境如何)或自定义二进制格式工作。

相关内容

最新更新