MailMessage发送字符串作为正文,而在outlook中没有换行符

嗨,我正在尝试使用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显示正确的换行信息?

Outlook有时会删除换行符(它通常会弹出一条注释,它已经完成了它),不确定它何时执行此操作的规则,但我很确定你是否添加了一个. (句号)在每一行的末尾它不会删除它们。

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

这对我有用

 mMailMessage.IsBodyHtml = true; Literal1.Text = "Dear Admin, 
You have recieved an enquiry onyour Website. Following are the details of enquiry:

Name: " + TextBox2.Text + "
Address: " + TextBox3.Text + ", " + TextBox4.Text + "
Phone: " + TextBox5.Text + "
Email: " + TextBox2.Text + "
Query: " + TextBox6.Text+"
Regards,
Veritas Team"; mMailMessage.Body = Literal1.Text;

你的字符串如下

 "Your string.\n" 

和:

 ms.IsBodyHtml = false; 

但是你可以在你的Junk文件夹中找到它

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

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

你应该使用字符串MyString + =“这是我的文字”+“%0d%0A”

此%0d%0A将被outlook识别为新行的转义序列。

//电子邮件正文应为HTML //用break HTML替换新行字符\ n \ r \ n

 mailMsg.IsBodyHtml = true; mailMsg.Body = this.Body; mailMsg.BodyEncoding = System.Text.Encoding.ASCII; mailMsg.Body = mailMsg.Body.Replace(Environment.NewLine, "
");

将IsBodyHTML设置为false:

ms.IsBodyHtml = false;

默认情况下它是假的,但看起来你已经将它设置为true,因为outlook认为它是HTML。

这对我有用:

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

小心不要在第2和第3行缩进。

我能够通过在问题行之前的行尾添加一些字符串空格来使其工作。

 msg.Append("\n some Message" + " "); msg.Append("\n another message"); 

Necro回答了一个问题,但也可以派上用场。

 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); 

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

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

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