December 2009 - Posts

SOLUTION: Outlook Is Stripping Line Breaks From Plain-Text Emails Auto-Generated From My ASP.NET Application!
08 December 09 11:36 AM | Scott Mitchell | 1 comment(s)

Today I was working on an ASP.NET application that sends out plain-text emails to site administrators when certain conditions unfold. One problem I unearthed today is that when viewing these emails in Microsoft Outlook many of the carriage returns were being removed, making the email difficult to read. The code to generate the body of these emails is very simple and straightforward, looking something like:

StringBuilder body = new StringBuilder();
body.Append("Information of interest:").Append(Environment.NewLine);
body.AppendFormat("Item #1: {0}", someValue).Append(Environment.NewLine);
body.AppendFormat("Item #2: {0}", someOtherValue).Append(Environment.NewLine);
body.AppendFormat("Item #3: {0}", yetAnotherValue).Append(Environment.NewLine);
...

When viewing the message in Outlook it would come out like so:

Information of interest:
Item #1: blah Item #2: blah blah Item #3: blah blah blah

Note how the carriage returns between each of the three items has been removed. Boo. The behavior is not due to the code that generates the email, but rather how Outlook displays a message. (For instance, GMail displays the carriage returns as you'd expect.) Outlook strips out what it identifies an unnecessary carriage returns. If you open the email in Outlook you'll see a little informational bar at the top of the email explaining that Outlook has removed extra line breaks and that you can click it to add them back in. Furthermore, you can configure Outlook to never remove such carriage returns by going to Tools --> Options, clicking the E-mail Options button, and then unchecking “Remove extra line breaks in plain text messages.“

Of course, it's never a good strategy to tell you customers that the problem is on their end and that they need to do something to fix the problem. The good news is that there appears to be a way that you can format the line breaks so that Outlook won't automatically remove them. I got this tip from member scarecrow-rye at the ASP.NET Forums, in the post Email text message line breaks not working:

Try including an empty space character at the end of each line. Certainlty seems to work for me.

And it worked for me, too. With this little adjustment to the code, Outlook stopped stripping out what it had earlier deemed as extra line breaks:

StringBuilder body = new StringBuilder();
body.Append("Information of interest:").Append(Environment.NewLine);
body.AppendFormat("Item #1: {0} ", someValue).Append(Environment.NewLine);
body.AppendFormat("Item #2: {0} ", someOtherValue).Append(Environment.NewLine);
body.AppendFormat("Item #3: {0} ", yetAnotherValue).Append(Environment.NewLine);
...

Note the extra space between the value and the end of the string.

December's Toolbox Column Now Online
01 December 09 12:52 PM | Scott Mitchell

My Toolbox column in the December 2009 issue of MSDN Magazine is available online and includes the following reviews:

  • Huagati DBML/EDMX Tools - a Visual Studio Add In that improves the LINQ to SQL and Entity Framework Designers, allowing you to quickly rename objects, update the object model to reflect the latest relational model changes, and auto-document the object model based on column descriptions in the database.
  • Blogs of Note: Oren Eini a/k/a Ayende Rahien - Oren Eini is the creator of Rhino Mocks and a contributor to the NHibernate project. On his blog you'll find posts covering a spectrum of topics, and his output - at times Oren writes three or more posts per day - is impressive. A must-read for .NET developers and architects.
  • SQL Multi Script - SQL Multi Script is a desktop application for executing one query against multiple databases. This tool is useful for DBAs who manage several databases and with Software as a Service-type applications that use separate databases for each customer.
  • Tabs Studio - a Visual Studio Add In that improves the tabbed user interface. Unlike VS's default tabbed interface, with Tabs Studio tabs are stacked vertically, if needed, so that you can see a tab for every open document. Tabs Studio also groups related documents into a single tab and the appearance of all tabs, the currently selected tab, and the previously selected tab can be customized.

There was no book review in this issue.

Enjoy! - http://msdn.microsoft.com/en-us/magazine/ee819138.aspx

Filed under:
More Posts

Archives

My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.