11

嗨,我正在尝试使用 system.net.mail.mailmessage 发送一个简单的通知。我只是将字符串作为消息正文传递。但问题是即使发送的多行消息在字符串中具有正确的“\r\n”格式信息。在 Outlook 中打开的邮件将显示为长单行。但是在 Outlook 中查看源代码会以正确的新行格式显示消息。

例如:原始消息如下所示:

line1
line 2
line 3

但它会显示在 Outlook 中,如:

line1 line 2 line 3

从 Outlook 中查看源代码仍将显示

line1
line 2
line 3

我应该怎么做才能使 Outlook 显示带有正确换行信息的消息?

4

10 回答 10

14

Outlook 有时会删除换行符(它通常会弹出一条评论说它也这样做了),不确定何时执行此操作的规则,但我相当确定您是否.在每个末尾添加一个(句号)行它不会删除它们。

实际上,看这篇文章,您似乎也可以通过将电子邮件作为 HTML 或 RTF 发送来解决它:在 Outlook 中以纯文本格式制作的帖子中删除了换行符

于 2010-10-14T08:34:45.637 回答
3

有你的字符串如下

"Your string.\n"

和:

ms.IsBodyHtml = false;

但你可能会在垃圾文件夹中找到它

于 2011-07-19T14:21:09.377 回答
3

这对我有用

mMailMessage.IsBodyHtml = true;
Literal1.Text = "Dear Admin, <br/>You have recieved an enquiry onyour Website. Following are the details of enquiry:<br/><br/>Name: " + TextBox2.Text + "<br/>Address: " + TextBox3.Text + ", " + TextBox4.Text + "<br/>Phone: " + TextBox5.Text + "<br/>Email: " + TextBox2.Text + "<br/>Query: " + TextBox6.Text+"<br/> Regards, <br/> Veritas Team";
mMailMessage.Body = Literal1.Text;
于 2012-10-28T19:53:33.530 回答
3

//电子邮件正文应该是 HTML //用中断 HTML 替换换行符 \n\r

mailMsg.IsBodyHtml = true;
mailMsg.Body = this.Body;
mailMsg.BodyEncoding = System.Text.Encoding.ASCII;
mailMsg.Body = mailMsg.Body.Replace(Environment.NewLine, "<br/>"); 
于 2015-10-13T19:13:36.260 回答
1

Outlook 将始终删除换行符,但是,可以使用以下内容:

而不是使用:Environment.Newline 或 \n\r

您应该使用字符串 MyString += "this is my text" + "%0d%0A"

这个 %0d%0A 将被 Outlook 识别为新行的转义序列。

于 2011-11-04T18:06:16.930 回答
0

将 IsBodyHTML 设置为 false:

ms.IsBodyHtml = false;

默认情况下它是假的,但看起来你已经在某处将它设置为真,因为 Outlook 认为它是 HTML。

于 2010-10-14T08:36:16.787 回答
0

这对我有用:

    mail.Body = String.Format(@"Some text here:
A new line here  
And another line here");

请注意不要在第 2 行和第 3 行缩进。

于 2015-06-04T15:09:51.793 回答
0

通过在有问题的行之前的行尾添加一些字符串空格,我能够让它工作。

msg.Append("\n some Message" + "   ");
msg.Append("\n another message");
于 2015-07-20T18:49:08.107 回答
0

死灵回答一个问题,但对某些人来说也可以派上用场。

msg.IsBodyHtml = true;
AlternateView av = AlternateView.CreateAlternateViewFromString(msg.Body, new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html));
av.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
msg.AlternateViews.Add(av);
AlternateView avPlain = AlternateView.CreateAlternateViewFromString(msg.Body, new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain));
avPlain.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
msg.AlternateViews.Add(avPlain); 
于 2016-07-21T07:50:05.947 回答
0

尝试@在您的消息之前使用逐字运算符“”:

message.Body = 
@"
line1
line 2
line 3
"

考虑到文本与左边距的距离也会影响到电子邮件正文左边距的实际距离。

于 2017-05-09T17:37:16.340 回答