以纯文本形式发送邮件时忽略换行符

发布于 2024-11-24 02:25:51 字数 117 浏览 0 评论 0原文

我有一条类似“嗨,\r\n 这是测试 \r\n 谢谢”的文本,我正在使用 MailMessage 类发送邮件。我已将“IsBodyHtml”属性设置为 false。问题是我收到的邮件没有换行符。你能让我知道我缺少什么吗?

I have a text like " Hi, \r\n this is test \r\n Thanks" I am sending the mail using MailMessage class. I have set the "IsBodyHtml" property to false. The issue is that I am receiving mails without line breaks. Can you let me know what I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

怪我入戏太深 2024-12-01 02:25:51

使用Environment.NewLinemsdn 而不是 \r\n。

Use Environment.NewLinemsdn instead of \r\n.

冷情 2024-12-01 02:25:51

我们遇到了同样的问题,但是如果您在 String 中一次性定义消息,而不是在 StringBuilder 中,则可以像这样定义消息

string message = string.Format(
@"First Line: {0}
Second Line: {1}
ThirdLine: {2}", firstValue, secondValue, thirdValue);

:像这样的 body 并设置 IsBodyHtml = false,将为您提供所需的新行。

否则,使用StringBuilder

var sb = new StringBuilder();
sb.AppendLine("FirstLine");
sb.AppendLine("SecondLine");

We had the same problem, but if you define your message all at once in a String, as opposed to a StringBuilder, you can define your message like this:

string message = string.Format(
@"First Line: {0}
Second Line: {1}
ThirdLine: {2}", firstValue, secondValue, thirdValue);

Defining the message body like this, and setting IsBodyHtml = false, will give you the new lines that you want.

Otherwise, use StringBuilder

var sb = new StringBuilder();
sb.AppendLine("FirstLine");
sb.AppendLine("SecondLine");
糖粟与秋泊 2024-12-01 02:25:51

这是 Outlook 中的一项功能,您可以在 Outlook 中将其关闭。转到选项 - 邮件 - 在“消息格式”下取消选中“删除纯文本消息中的额外换行符”。

另一个解决方案是发送邮件时在每行末尾添加三个空格。这似乎让 Outlook 接受这不是额外的换行符。

This is a feature in Outlook, you can turn it off in Outlook. Go to Options - Mail - and under "Message Format" you uncheck "Remove extra line breaks in plain text messages".

Another solution is to add three spaces at the end of each line, when you send the mail. This seems to get Outlook to accept that it is not an extra line break.

仙女山的月亮 2024-12-01 02:25:51

如果您正在从 Outlook 阅读邮件,则 Outlook 可能会删除换行符,认为它们是额外的换行符。您是否尝试过从其他软件(或者网络邮件)阅读邮件?

If you are reading your mails from Outlook, it may be Outlook that is removing line breaks, thinking they are extra line breaks. Did you try reading your mails from another software - or maybe a webmail?

趴在窗边数星星i 2024-12-01 02:25:51

我认为,为了能够在邮件中加入换行符,而不仅仅是简单的测试,您需要将正文 html 设置为 true。

To be able to incorporate line breaks, rather than just plain test in your mail, you will need to have the body html set to true, I think.

夏九 2024-12-01 02:25:51

我必须处理的一个更棘手的原因可能会发生:

  • 您的邮件服务器操纵传出消息并将 html 版本添加到您原本仅包含文本的消息中

A more tricky reason this may happen that I just had to deal with:

  • Your mail server manipulates outgoing messages and adds a html version to your otherwise text only message
嘿看小鸭子会跑 2024-12-01 02:25:51

我遇到了类似的问题,大部分换行符都有效,但有一个却无效。如果我点击回复未显示换行符的电子邮件,则回复下方的原始电子邮件文本将显示换行符(因此这表明这是 Outlook 问题)。我尝试了Environment.NewLine的推荐答案,但没有改变任何东西。就我而言,在我想要新行的语句末尾添加一个句点,然后放入 \r\n 就可以了。根据我在此讨论中发布的先前链接,Outlook 正在使用一些规则来过滤换行符,并且在开始此讨论的问题中,我注意到您的句子末尾没有句号。

I was having a similar problem where most of my line breaks were working but one was not. If I hit reply to the email that wasn't showing the line breaks, the original email text below the reply would show the line break (so this indicates it is an outlook issue). I tried the recommended answer of Environment.NewLine and that DID NOT change anything. In my case, adding a period at the end of the statement where I wanted the new line and then putting in a \r\n did the trick. Per a previous link I posted in this discussion, Outlook is using some rules to filter out line feeds and in the question that started this discussion I notice you do not have a period at the end of your sentence.

长梦不多时 2024-12-01 02:25:51

我通过 PowerShell 脚本发送电子邮件通知,在 Outlook 中查看邮件时也没有出现任何换行符。
对我来说 .ps1 文件中的解决方案是这样的:

$emailMessage.Body = "First Line" + "`r`n" + "Second Line"

I was sending E-Mail notification via PowerShell script and also did not get any line breaks when viewing the mail in Outlook.
For me the solution in the .ps1 file was this:

$emailMessage.Body = "First Line" + "`r`n" + "Second Line"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文