当我进行替换时,为什么 C#.net 会删除换行符?如何阻止它?
我有一个文本区域保存到数据库中,我用它作为电子邮件正文发送。
我允许将令牌用作与该消息相关的信息的占位符。
如果我根本不触摸占位符,则电子邮件会很好地发送,换行符与文本框中的完全相同(电子邮件以纯文本形式发送)。
但是,当我开始使用替换功能时,新行字符开始消失,并且所有行都被推到一起。
例如。
Body.Replace("%procedure%", CurrentOrder.Description);
将替换文本 %procedure%,但也会删除行尾的换行符。即使换行符不是直接在被替换的文本之后。
有什么想法吗?
编辑:
目前,我只是将“\n”替换为“
”并以 HTML 形式发送电子邮件。我宁愿将其保留为纯文本,因为我根本无法控制收件人。
编辑2:这似乎是 Outlook 本身的问题,而不是电子邮件的问题。我刚刚在 gmail 中查看了完全相同的电子邮件,并且格式正确。
I have a textarea saving to a database that I'm using to send as the body of an email.
I allow tokens to be used as placeholders for information pertaining to that message.
If I don't touch the placeholders at all the email sends just fine with the line breaks exactly as they are in the textbox (the email is being sent in plain text).
However, when I start using the replace function, the new line characters start disappearing and all the lines get pushed together.
For example.
Body.Replace("%procedure%", CurrentOrder.Description);
Will replace the text %procedure%, but will also remove the newline at the end of the line. Even if the newline isn't directly after the text being replaced.
Any ideas?
edit:
For now, I'm just replacing "\n" with "<br />
" and sending the email as HTML. I would rather keep it as plain text as I don't have control over the recipients at all.
EDIT 2: It appears to be an issue with outlook itself, not the email. I just viewed the exact same email in gmail, and the format was correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Outlook 会删除新行,除非该行以两个空格结尾。
如果您正在测试发送到使用 Outlook 的帐户的电子邮件,请尝试在新行之前添加两个空格,看看是否可以解决问题。
Outlook removes new lines unless the line ends with two spaces.
If you're testing the emails to an account that uses Outlook try adding two spaces before your new lines and see if that fixes it.
如果它用于 HTML,我首先会用 BR 标记替换新行:
或者使用 JQuery 或 AJAX HTML 编辑器或其他东西,而不是使用多行文本区域。
也许你可以交换一个占位符(像上面一样,然后把它交换出来?)
If its for HTML I would first of all replace the new lines with BR tags:
either that, or instead of using a multiline textarea, use a JQuery or AJAX HTML Editor or something.
Maybe you could swap in a place holder (like above, and then swap it out?)