MailMessage 将字符串作为正文发送,在 Outlook 中不带换行符

发布于 2024-09-27 13:51:29 字数 417 浏览 6 评论 0原文

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

HI, I am trying to send a simple notification using system.net.mail.mailmessage. I just pass the string as the message body. But problem is even though multi-line message been send have the correct "\r\n" format info in string. The mail
opened in outlook will displayed as a long single line. But view source in outlook will display the message with correct new line format.

For example:
original message would looks like:

line1
line 2
line 3

But it will displayed in Outlook like:

line1 line 2 line 3

View source from outlook will still display

line1
line 2
line 3

What should I do to make outlook display the message with correct newline information?

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

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

发布评论

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

评论(10

星星的軌跡 2024-10-04 13:51:29

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

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

Outlook sometimes removes newlines (it usually pops up a comment that it has done it as well), not sure exactly about the rules for when it does this but I'm fairly sure if you add a . (full stop) at the end of each line it won't remove them.

Actually, looking at this article it seems like you can also solve it by sending the emails as HTML or RTF: Line breaks are removed in posts made in plain text format in Outlook

一个人的旅程 2024-10-04 13:51:29

让你的字符串如下所示

"Your string.\n"

并且:

ms.IsBodyHtml = false;

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

Have your string like below

"Your string.\n"

And:

ms.IsBodyHtml = false;

But you may get it in your Junk folder

廻憶裏菂餘溫 2024-10-04 13:51:29

这对我有用

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;

This worked for me

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;
满栀 2024-10-04 13:51:29

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

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

//the email body should be HTML
//replaces the new line characters \n\r with the break HTML

mailMsg.IsBodyHtml = true;
mailMsg.Body = this.Body;
mailMsg.BodyEncoding = System.Text.Encoding.ASCII;
mailMsg.Body = mailMsg.Body.Replace(Environment.NewLine, "<br/>"); 
情未る 2024-10-04 13:51:29

Outlook 总是会删除换行符,但是,可以使用以下内容:

而不是使用:
环境.换行符
或者
\n\r

你应该使用
string MyString += "this is my text" + "%0d%0A"

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

Outlook will always remove line breaks, however, what can be used is the following:

In stead of using:
Environment.Newline
or
\n\r

You should use
string MyString += "this is my text" + "%0d%0A"

This %0d%0A will be recognised by outlook as the escape sequence for a new line.

紧拥背影 2024-10-04 13:51:29

将 IsBodyHTML 设置为 false:

ms.IsBodyHtml = false;

默认情况下,它是 false,但似乎您已在某处将其设置为 true,因为 Outlook 认为它是 HTML。

Set the IsBodyHTML to false:

ms.IsBodyHtml = false;

By default it is false but it appears you have set it to true somewhere as outlook thinks it is HTML.

悲凉≈ 2024-10-04 13:51:29

这对我有用:

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

只要小心不要在第二行和第三行缩进。

This worked for me:

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

Just be careful to not indent on the 2nd and 3rd line.

温柔戏命师 2024-10-04 13:51:29

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

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

I was able to get it to work by adding a few string spaces at the end of line prior to the troubled line.

msg.Append("\n some Message" + "   ");
msg.Append("\n another message");
萌无敌 2024-10-04 13:51:29

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

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

Necro ansering a question but could come in handy for some as well.

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); 
可是我不能没有你 2024-10-04 13:51:29

尝试在邮件前使用逐字运算符“@”:

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

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

Try using the verbatim operator "@" before your message:

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

Consider that also the distance of the text from the left margin affects on the real sistance from the email body left margin..

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