繁体   English   中英

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

[英]MailMessage sent string as body without newline in 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中以纯文本格式发布的帖子中删除换行符

这对我有用

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;

//电子邮件正文应为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, "<br/>"); 

你的字符串如下所示

"Your string.\n"

和:

ms.IsBodyHtml = false;

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

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

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

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

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

这对我有用:

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

将IsBodyHTML设置为false:

ms.IsBodyHtml = false;

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM