.net Outlook 自动清理我的换行符并弄乱我的电子邮件格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/247546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Outlook autocleaning my line breaks and screwing up my email format
提问by Aheho
I'm sending an email using the dotnet framework. Here is the template that I'm using to create the message:
我正在使用 dotnet 框架发送电子邮件。这是我用来创建消息的模板:
Date of Hire: %HireDate%
Annual Salary: %AnnualIncome%
Reason for Request: %ReasonForRequest%
Name of Voluntary Employee: %FirstName% %LastName%
Total Coverage Applied For: %EECoverageAmount%
Guaranteed Coverage Portion: %GICoveragePortion%
Amount Subject to Medical Evident: %GIOverage%
When the messages is received in outlook, outlook tells me "Extra line breaks in this message were removed". And the message displays like this:
在 Outlook 中收到消息时,Outlook 会告诉我“此消息中的额外换行符已删除”。消息显示如下:
Date of Hire: 9/28/2001
Annual Salary: 0,000
Reason for Request: New Hire
Name of Voluntary Employee: Ronald Weasley Total Coverage Applied For: 0,000 Guaranteed Coverage Portion: 0,000.00 Amount Subject to Medical Evident: 0,000
Note how Outlook incorrectly removes needed line breaks after the name, EECoverageAmount, etc...
请注意 Outlook 如何在名称、EECoverageAmount 等之后错误地删除所需的换行符...
It's important for the email recepients to get a correctly formatted email, and I have to assume that some of them use outlook 2003. I also can't assume they will know enough to shutoff the autoclean feature to get the message to format properly.
电子邮件收件人获得格式正确的电子邮件很重要,我必须假设他们中的一些人使用 Outlook 2003。我也不能假设他们会关闭自动清理功能以正确设置邮件格式。
I have viewed these messages in other mail clients and they display correctly
我在其他邮件客户端中查看过这些消息,它们显示正确
some more information:
更多信息:
- I am using UTF-8 BodyEncoding (msg.BodyEncoding = System.Text.Encoding.UTF8)
- The msg.Body is being read from a UTF-8 encoded text file, and each line is terminated with a crlf.
- 我正在使用 UTF-8 BodyEncoding (msg.BodyEncoding = System.Text.Encoding.UTF8)
- msg.Body 正在从 UTF-8 编码的文本文件中读取,每一行都以 crlf 结尾。
Question: How do I change the format of the message to avoid this problem?
问题:如何更改消息的格式以避免此问题?
采纳答案by Alex B
Start every line with 2 spaces and outlook will be "tricked" into keeping your formatting.
以 2 个空格开始每一行,Outlook 将被“欺骗”以保持您的格式。
So change
所以改变
Date of Hire: %HireDate%
Annual Salary: %AnnualIncome%
Reason for Request: %ReasonForRequest%
Name of Voluntary Employee: %FirstName% %LastName%
Total Coverage Applied For: %EECoverageAmount%
Guaranteed Coverage Portion: %GICoveragePortion%
Amount Subject to Medical Evident: %GIOverage%
to
到
Date of Hire: %HireDate%
Annual Salary: %AnnualIncome%
Reason for Request: %ReasonForRequest%
Name of Voluntary Employee: %FirstName% %LastName%
Total Coverage Applied For: %EECoverageAmount%
Guaranteed Coverage Portion: %GICoveragePortion%
Amount Subject to Medical Evident: %GIOverage%
^^ <--- Two extra spaces at the start of every line
Here is the articleI found when researching this problem which goes into a little more depth than my answer.
这是我在研究这个问题时发现的文章,它比我的答案更深入。
回答by Jim Davis
You can also insert a tab character at the endof the line (just before the CR LF). This extra white space will be at the end of the line and hence not visible to user. You might prefer this to having to insert spaces on the left. Note that a single space is not enough (though perhaps multiple spaces would help, I don't know.)
您还可以插入在一个制表符结束行(就在CR LF之前)。这个额外的空白将在行尾,因此对用户不可见。与必须在左侧插入空格相比,您可能更喜欢这样做。请注意,一个空格是不够的(尽管多个空格可能会有所帮助,我不知道。)
回答by DHornpout
This answer is on how to "disable" the feature from the Outlook Client.
此答案是关于如何从 Outlook 客户端“禁用”该功能。
- Go to Tools -> "Options ..."
- In the "Preferences" tab click on "Email Options ..."
- Uncheck the box "Remove extra line breaks in plain text messages."
- Hit OK
- 转到工具->“选项...”
- 在“首选项”选项卡中,单击“电子邮件选项...”
- 取消选中“删除纯文本消息中的额外换行符”框。
- 点击确定
FYI:I am using Outlook 2007
仅供参考:我正在使用 Outlook 2007
回答by supernova
Adding "\t\r\n" ( \t for TAB) instead of "\r\n" worked for me on Outlook 2010.
在 Outlook 2010 上添加“\t\r\n”(\t 表示 TAB)而不是“\r\n”对我有用。
回答by Doug L.
I have always had better luck formatting e-mails as html. You may still have the end-user issue of having to set the client to allow html format, but they are usually more familiar with this since so many e-mails do come html formatted. You also have a little more work on your end adding the html tags, but the end result is much more controllable.
我总是有更好的运气将电子邮件格式化为 html。您可能仍然遇到必须将客户端设置为允许 html 格式的最终用户问题,但他们通常更熟悉这一点,因为很多电子邮件确实是 html 格式的。您还需要做更多的工作来添加 html 标签,但最终结果更加可控。
@ephemient also suggests: Send as both HTML and plaintext. Good clients will show the latter, Outlook will show the former, everybody is happy (except the programmer who has to do more work).
@ephemient 还建议:以 HTML 和纯文本形式发送。好的客户会显示后者,Outlook 会显示前者,每个人都很高兴(除了必须做更多工作的程序员)。
回答by Ho Ho Ho
Expanding on the Doug Lanswer, since I think HTML messaging is even more ubiquitous and accepted now than in 2008 when that answer was posted. Here is a little c# snippet to help convert the body and send the message in HTML format:
扩展Doug L 的答案,因为我认为 HTML 消息传递现在比 2008 年发布该答案时更加普遍和被接受。这是一个小 c# 片段,可帮助转换正文并以 HTML 格式发送消息:
body = string.Format("<font face='calibri,arial,sans-serif'>{0}<font/>", body.Replace("\r\n", "<br>"));
using (var smtpClient = new SmtpClient() { Host = smtpHost })
using (var msg = new MailMessage(from, emailDistribution, subject, body) { IsBodyHtml = true })
smtpClient.Send(msg);
回答by Brijesh Kumar Tripathi
Auto cleaning is an outlook feature which removes extra line breaks. You can prevent it by two ways.
自动清理是一项 Outlook 功能,可删除额外的换行符。您可以通过两种方式阻止它。
First wayis to add at least 2 extra spaces before lines while sending the email through code. You can add these extra spaces only before those lines which outlook automatically removes.
Second wayis to change outlook setting which will prevent outlook from removing extra line breaks.
第一种方法是在通过代码发送电子邮件时在行前添加至少 2 个额外空格。您只能在 Outlook 自动删除的那些行之前添加这些额外的空格。
第二种方法是更改 Outlook 设置,这将防止 Outlook 删除额外的换行符。
回答by Christian Casutt
Put the text in <pre>Tags and outlook will format and display the text correctly.
将文本放入<pre>标签,Outlook 将正确格式化和显示文本。
i defined it in CSS inline in HTML Body like:
我在 HTML Body 中内联的 CSS 中定义了它,例如:
CSS:
CSS:
pre {
font-family: Verdana, Geneva, sans-serif;
}
i defined the font-family to have to font set.
我定义了字体系列必须设置字体。
HTML:
HTML:
<td width="70%"><pre>Entry Date/Time: 2013-09-19 17:06:25
Entered By: Chris
worklog mania
____________________________________________________________________________________________________
Entry Date/Time: 2013-09-19 17:05:42
Entered By: Chris
this is a new Worklog Entry</pre></td>
回答by Christian Casutt
I'm seeing the same problem when generating a plain-text email and then reading it with Outlook 2003 SP3. It appears you can avoid the removal process by it by keep the line length under 40 characters. May not always be practical.
我在生成纯文本电子邮件然后使用 Outlook 2003 SP3 阅读时遇到了同样的问题。看来您可以通过将行长度保持在 40 个字符以下来避免删除过程。可能并不总是实用的。
回答by Keyur Patel
My text includes '\r\n' but Outlook 2010 does not render line break. Create tokens of lines delimited by '\r\n' and envelope tokens by HTML Paragraph tags. My Email format is HTML. I am generating HTML Body for my email in the code below.
我的文本包含 '\r\n' 但 Outlook 2010 不呈现换行符。创建由 '\r\n' 分隔的行标记和 HTML 段落标记的信封标记。我的电子邮件格式是 HTML。我在下面的代码中为我的电子邮件生成 HTML 正文。
string[] tokens = Regex.Split(objTickt.Description, "\r\n");
if (tokens.Length > 0)
{
foreach (string line in tokens)
{
//htmlTW.WriteEncodedText(objTickt.Description.Replace("\r\n", "\n\n"));
htmlTW.RenderBeginTag(HtmlTextWriterTag.P);
htmlTW.WriteEncodedText(line);
htmlTW.RenderEndTag();
}
}


