0

When I try to create the email body from my C# program some funny things happen

oItem.Body = text;

oItem is a Microsoft.Office.Interop.Outlook.MailItem and text is just an ordinary string containing \n When I do this, the \n gets translated into \r\n. The problem is that some of the \n get translated into \r\n\r\n and I cannot figure out why or when. The content of text comes from different sources but it is just a normal string, like in the example below

text: "Zusammenfassung:\nText0\nText1 \n\nText2\nText3\nText4 {...} TextX\n 

oItem.Body: "Zusammenfassung: \r\nText0 \r\nText1 \r\n\r\nText2\r\n\r\nText3\r\n\r\nText4 {...} TextX\r\n 

What is happening there? I'm totally confused.

edit: the double newline is created when the string is build with +=

string lineToSend = "bla Bla: " + Sig + " bla bla " + string.Join(", ", usedCnt.toCntName());
if (refCnts.Count() != 0)
{

    lineToSend += "some Text";
}
else 
{
    lineToSend += " some other Text";
}

//lineToSend = "dummyStringLine";
messageObj.setMessageLines(lineToSend);

If i use the dummyStringLine everything is fine.

  • 1
    Me too, does assigning a `string` to a `MailItem` work? – Jodrell Dec 12 '12 at 15:43
  • my fault i just misstyped the code snippet of Course i try to set oItem.body – Nerevarine Dec 12 '12 at 15:46
  • @Nerevarine instead of manually entering \n, try using Environment.NewLine instead. See oif that helps. – Ganesh R. Dec 12 '12 at 15:52
  • @Nerevarine if you're going to edit your question, edit it properly. Don't rollback to crappy revisions, and don't ignore edits to your post made by seasoned users. `quick:` is not a format anyone on this site uses/encourages to describe a problem. Please don't do that. – jcolebrand Dec 12 '12 at 15:59
  • @GaneshR. Just tried it, the problem persists (of course now text has one `\r\n` instead of `\n`) – Nerevarine Dec 12 '12 at 16:09
  • Only one? Replace all the instances. – jcolebrand Dec 12 '12 at 16:11

1 Answers1

0

Just to make it easier to lex out where the places are ... are you sure that this is an exact duplication?

"Zusammenfassung:\nText0\nText1 \n\nText2\nText3\nText4 {...} TextX\n 
                 ^1     ^2      ^3^4     ^5     ^6                 ^7
"Zusammenfassung: \r\nText0 \r\nText1 \r\n\r\nText2\r\n\r\nText3\r\n\r\nText4 {...} TextX\r\n 
                 ^1        ^2         ^3  ^4       ^5           ^6                       ^7

If so, you've clearly stumped me, however, I expect that some things are slightly different between your supposed two sources, and you missed it, or the like.

I can, however, answer the first part, and I shall do so: It's replacing \n with \r\n which is a standard "non-Windows -> Windows line ending" conversion process. Where is the text getting the \n from in the first place? If from your code, I suggest you try inserting Environment.NewLine instead of \n in your own code.

If you're getting the file from an outside source, I recommend prescrubbing the input before giving it to the Outlook element.


StringBuilder example:

StringBuilder sb = new StringBuilder();

sb.AppendLine("Zusammenfassung:").AppendLine( text0 ).AppendLine( text1 ).AppendLine( Environment.NewLine );
sb.AppendLine( text2 );
sb.Append( text3 ).Append( Environment.NewLine );
//I'm sure you get the idea here

oItem.Body = sb.ToString();

See if by doing this, you get the same errors ... I realize it's a bit of refactoring, but maybe you can just dummy up part of the method to make it faster?


using your code ...

StringBuilder sb = new StringBuilder("bla Bla: ");

sb.Append( Sig ).Append( " bla bla " ).Append( string.Join(", ", usedCnt.toCntName()) );
if (refCnts.Count() != 0)
{

    sb.Append( "some Text" );
}
else 
{
    sb.Append( " some other Text" );
}

//lineToSend = "dummyStringLine";
messageObj.setMessageLines(sb.ToString());
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
  • I just double checked the strings ind n++ and they are indeed equal. I changed the `\n` to `Environment.NewLine` nevertheless. I will try to kick sensible information out of the Strings so I can show you the exact Strings – Nerevarine Dec 12 '12 at 16:12
  • Okay i did the above mentioned. IF i add dummyStrings. the email body looks just fine. I narrowed the problem to some lines of code. The problem appears, if the string line was created using `+=` in it. – Nerevarine Dec 12 '12 at 16:37
  • That is highly interesting. You should additionally change your code to use StringBuilders .. if you put some of your code as a sample (where you use the +=) I'll help you rewrite that part. I'll add a basic from->to in the answer. – jcolebrand Dec 12 '12 at 19:18
  • okay i tried it. It gets weirder.. C# Debug of the Strings created is here http://imageshack.us/photo/my-images/100/stringbehavior.jpg/ I used the String Builder. and narrowed it down to one line of code `sb.Append("some other Text").Append("lastText"); // this one works fine` `sb.Append(" wird in verschiedenen Versionen im Projekt genutzt"); // this doesn't` I'll be on a buisness trip until monday but i really appreciate your help, thanks – Nerevarine Dec 13 '12 at 10:51
  • So here are all 4 debugs: http://img254.imageshack.us/img254/2392/stringbehavior.png – Nerevarine Dec 13 '12 at 11:02