如何使用MVC smtp消息体在web电子邮件中显示换行符



我要么疯了,要么之前没有我能理解的答案(这可能发生)。

Gmail在电子邮件正文中显示了这一点(在devbox上使用IIS,因此链接中为"localhost"):

请单击此链接确认您的帐户:http://localhost:53891/Account/ConfirmEmail?userId=526b54e1-2275-4bb0-bc8347289f413a69&代码=ErXrRF8f%2PZFK43WxwxQJjfT7%2BIIgd6HhglTNq8btRDdRZwbBxgwtmtuGhBDozgr9vyy2cy%2Bs0weateY4Hgv6BmjTOK7fJ7S2rS%2B8ShjhILM7kXZNqMBK4sTGSHoSozURZRgfTQcSB2OBgTFk8v%2BvlslF5Xsilz%2Bd0OwnJz6KwOqNzzMrB6DgUyjUAIOJTt或单击并在浏览器中复制链接。

当我点击它并注册我时,它就工作了,但我似乎无法格式化它,所以它有换行符/换行符。魔鬼在细节中哈哈!

这是我发送电子邮件的MVC IdentityConfig.cs:

               string html = HttpUtility.HtmlEncode("Please confirm your account by clicking this link: " + 
Environment.NewLine + Environment.NewLine + message.Body);
               html += HttpUtility.HtmlEncode(Environment.NewLine);
               html += HttpUtility.HtmlEncode(Environment.NewLine);
               html += HttpUtility.HtmlEncode(Environment.NewLine);
               html += HttpUtility.HtmlEncode("Or click on and copy the link in your browser.");
               html.Replace(Environment.NewLine, "<br />");

您没有为html提供替换。

html = html.Replace(Environment.NewLine, "<br />");

对吧?

编辑:进一步的例子-https://dotnetfiddle.net/slm90h

    string html = "";
    html += Environment.NewLine;
    html += Environment.NewLine;
    html += Environment.NewLine;
    html += Environment.NewLine;
    html += Environment.NewLine;
    html.Replace(Environment.NewLine, "<br />");
    Console.WriteLine(html);
    Console.WriteLine("^---- Doesn't work");
    html = html.Replace(Environment.NewLine, "<br />");
    Console.WriteLine(html);
    Console.WriteLine("^----- Does work");

是否设置了IsBodyHtml=true?

叹气。这比我想象的要简单。并且不需要htmlEncode,因为html在电子邮件页面中,而不是网页中。(我认为这是有道理的)。我去掉了所有的Environment.Newline和htmlEncode,只放了一个html中断。

   string html = "Please confirm your account by clicking this link: " + "<br /><br />" + message.Body;
   html += "<br /><br /><br />";
   html += "Or click on and copy the link in your browser.";

以下是电子邮件正文中的内容(所有中断都发生了):

请点击以下链接确认您的账户:

http://localhost:xxxxx/Account/ConfirmEmail?userId=c56dfcd7-e63e-4a08-8c38-16419d8ff1c9&代码=WgoxvI%2FdCUjMgJwnXgZ3siP1vKSE1%2BuJ5SCX%2FjL3GPwk7kmycRrdVeNDygl65gQ3HuOlmid1YKNaX6pRhBTNuBDk0dMM7w%2F%2OnC%2BkE1vXHRDV1cv9Fcifk4i51ZXQef9%2BoKVfCPeqjYZoDPnQBsxZzYJEiVSSn8NvEBdjYG%2FpE1CM7QeShH9oDB2XJ1nFC

或者单击并在浏览器中复制链接。

相关内容

最新更新