Saving Outlook mails to text documents introduces unwanted extra line feeds

When I save the Body of a System.Net.Mail.Message (from Outlook) with the Write text file or the Append Text to Word activity, somehow extra line returns are inserted if the Mail object was of HTML encoding.
This problem is not relevant in mails that are not HTML.
But in my case all mails are HTML, so I need a solution to save the mail to a document file without introducing extra line feeds.
I have uploaded an image that shows my for each item in outlook, which entails the output to word and text-files, and below that the original e-mails in respectively HTML and Plain Text format.
Below that you can see the output of the activites, where the HTML mails are saved with lots of extra line breaks :open_mouth:

Any ideas?


If you mean that there are extra lines on the bottom, then you could try doing a .Trim on the text which would remove the empty lines. Or maybe I misunderstood.

1 Like

You have permission to post in RPA Dev - Rookies category. If there is an important topic it will be moved in How to or Issues category.

I just wanted to keep Issues and How To with relevant posts and not duplicates or minor/basic questions(as much as possible). Thanks for understanding!

ClaytonM, perhaps the new screenshot I uploaded is a better description of what I am experiencing?

Have you tried using


If Trim doesn’t do it, another option is to remove all blank lines
String.Join(System.Environment.Newline, item.Body.Split(System.Environment.Newline(0)).Where(Function(x) x.ToString.Trim<>"").ToArray)


Hi Clayton, the item.Body.Trim did not work, but the String.Join did. But the String.Join code snippet seems to remove ALL blank lines, which “screws” up the format of the original mail.
I don’t understand if this introduction of extra line breaks is a UiPath issue or a .NET System.Net.Mail.MailMessage issue… :o)
If you look at the screen shot embedded here from Word, These symbols seem to represent the line breaks, if I could do a search and replace on those, perhaps I could restore the mail body to look somewhat like the original mail. Can the snippet you provided do that? Capture2

Yeah, sounds like that line break is outside of a Newline, therefore the .Trim won’t work. You could probably do a replace on the very last System.Environment.Newline. There’s probably some info online on how to replace the last character in a string.

If you still don’t get an answer on this, I can get back to you later.


You can try this line that takes the string up to the index of the last occurrence of the newline character.


That is assuming the last character is always a new line, so if not then you might need to use a second substring to concatenate the string from the lastindex to the end.

Hope that works. Regards.

Thanks for your help ClaytonM, but in the end I had to take a different approach to ensure prints that look somewhat like the original mail. It does bug me though, that the object is so different to the original mail.
I opted to save the mails as files, then from the UI print them out individually.
I can’t recommend this, as the .eml files output from UiPath are a pain to work with compared to Outlooks native .msg files. But at least it works.

1 Like

Hi @Mikkel_Nielsen,

I’m also having trouble with this, can you explain to me how you did it.
I need to retain the format of the body of email but the UIPath gives me a lot of trouble in doing this. Can you share your work around on how you did this?
Many thanks.


I am facing a similar issue. I am copying tables from an Excel to a Word document and unnecessary lines are getting added in between. Is there anyway to fix this?
I checked the Excel and there are no extra empty lines there.