Replacing text in a huge string without memory leak
===

I am currently working on a batch that must generate about 16000 emails in a row (a newsletter).

Either it is spam or not, my question is about how I generate those e-mails.

Some fields in the message must be replaced by custom values (date of the day, name of the user, etc).

For some deadline and code-reusability reasons my template is an HTML file with some "_FIELDNAME" fields that can be easily spotted by a regex :
        
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    ...
    <body>
    <p>Hi _NAME, _DATE newsletter.</p>
    </body>
    ...

The file is about ~1000 lines so it is quite a big string when loaded.

First thing, I load once the HTML file template in a string :

    string template = File.ReadAllText(@"Template/newsletter.html");

And the replacing function looks like this :
        
    return new StringBuilder(template)
    .Replace("_DATE", profileConfig.SelectedMonth.ToString("MMMM yyyy"))
    .Replace("_NAME", profileConfig.Name)
    .ToString();

The problem is that the memory consumption increase slightly over each iteration. It's about 50MB for 1000 iterations, and it's due to my replacing function (I tried to comment it and the memory leaks disappeared).

How can I replace many fields (~50) in my template without overflowing the memory for my 16000 iterations ?
I tried a couple a thing, like using Regex (but it's using string) or temporary files but both didn't satisfied me.

Thanks in advance for your help.
