views:

4337

answers:

7

I'm sending an email using the dotnet framework. Here is the template that I'm using to create the message:

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:

Date of Hire: 9/28/2001
Annual Salary: $100,000
Reason for Request: New Hire

Name of Voluntary Employee: Ronald Weasley Total Coverage Applied For:  $500,000 Guaranteed Coverage Portion: $300,000.00 Amount Subject to Medical Evident: $200,000

Note how Outlook incorrectly removes needed line breaks after the name, EECoverageAmount, etc...

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.

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.

Question: How do I change the format of the message to avoid this problem?

A: 

Change your line termination from crlf to either cr or lf.

I suspect that the top of the email uses only cr (or lf), and Outlook expects the rest of the email to follow the same format.

seanyboy
I tried changing the line terminator to a CR. That didn't work. Then I tried changing it to a LF. That didn't work either.
Aheho
Doubtful. Outlook has a highly dubious feature which tries to convert mail with hard line breaks into flowed text. Unsurprisingly, it frequently gets this wrong, as in this case.
Daniel Cassidy
+2  A: 

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.

@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).

Doug L.
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
BTW, s/http/html/g in your answer.
ephemient
+7  A: 

Start every line with 2 spaces and outlook will be "tricked" into keeping your formatting.

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 article I found when researching this problem which goes into a little more depth than my answer.

Alex B
that worked. thanks!
Aheho
Here's a handy Regex to sort this out: `Regex.Replace(messageBody, @"^(?!\s\s)", " ", RegexOptions.Multiline);`
Wilfred Knievel
Ok, in that regex there are two spaces but this being html it's only displaying one!
Wilfred Knievel
A: 

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.

+5  A: 

You can also insert a tab character at the end of 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.)

Jim Davis
+1 thank you for this one, it seems much cleaner than prefixing the 2 spaces
wheelibin
`Regex.Replace(messageBody, @"(?<!\t)((?<!\r)(?=\n)|(?=\r\n))", "\t", RegexOptions.Multiline)`
Wilfred Knievel
+2  A: 

This question also occurs here and here.

+1  A: 

This answer is on how to "disable" the feature from the Outlook Client.

  • 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

DHornpout
I don't like this feature, either. Among other things, it causes code in emails to be broken and PGP signature checks to fail.
Brian Reiter