9

I have a small application written in c# as a console app that I want to use to send an email. I was planning on storing the email inside an xml file along with other information that the message will need like a subject. However there seems to be a problem because the XML file doesnt like </br> characters.

Im wondering what I should do in order to store a html email do I just have to keeo the body html in a seperate html file and then read each line into a StreamReader object?

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Exitos
  • 29,230
  • 38
  • 123
  • 178

4 Answers4

15

The easiest way would be to store the HTML content in a CDATA section:

<mail>
  <subject>Test</subject>
  <body>
    <![CDATA[
      <html>
        ...
      </html>
     ]]>
  </body>
</mail>
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
4

Use a CDATA section, that will contain your email HTML code :

<?xml version="1.0"?>
<myDocument>
  <email>
    <![CDATA[
        <html>
          <head><title>My title</title></head>
          <body><p>Hello world</p></body>
        </html>
    ]]>
  </email>
</myDocument>
mathieu
  • 30,974
  • 4
  • 64
  • 90
3

You can use CDATA section in your XML - here you can read about it.

Rafal Spacjer
  • 4,838
  • 2
  • 26
  • 34
3

You could store the HTML as CDATA within the XML.

But looking at what you are trying to do, you may wish instead look at the System.Web.UI.WebControls.MailDefinition class, as it already contains a reasonable way of using mail templates.

The msdn documentation gears towards it being used in WinForms apps, but you can simply use a ListDictionary to fill the replacements.

Here is a simplistic example, to give an idea of how MailDefinition can be used, I won't go into to much detail, as it's a little outside of the scope of the original question.

    protected MailMessage GetNewUserMailMessage(string email, string username, string password, string loginUrl)
    {
        MailDefinition mailDefinition = new MailDefinition();

        mailDefinition.BodyFileName = "~/mailtemplates/newuser.txt";

        ListDictionary replacements = new ListDictionary();

        replacements.Add("<%username%>", username);
        replacements.Add("<%password%>", password);
        replacements.Add("<%loginUrl%>", loginUrl);


        return mailDefinition.CreateMailMessage(email, replacements, this);



    }
Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
  • I don't think this is a good idea. It doesn't store the body in the object but in a separate file. – Daniel Hilgarth May 12 '11 at 13:05
  • Hi @Daniel Hilgarth true it does mean keeping it in a separate file, but it does make a very convenient implementation. I'll edit my answer with an example. – Alex KeySmith May 12 '11 at 13:13
  • I fail to see how it is convenient to store the data of the body in a separate file. Additionally, how does this match the OP's question about storing all data of an email in one xml file? More: Does it even support HTML? – Daniel Hilgarth May 12 '11 at 13:18
  • well its a useful class but I will probably stick to 1 xml file where I can put absolutely everything. This would be good for some situations thanks! – Exitos May 12 '11 at 13:32
  • @Pete2k , no worries, hmmm I wonder if MailDefiniation can be expanded to store in an xml file.... perahaps for another day :-) – Alex KeySmith May 12 '11 at 14:35
  • Hi @Daniel Hilgarth sorry your right the MailDefinition part isn't a direct answer, the first line was the direct answer. I just added the MailDefinition as additional useful information. I have implemented similar systems using e-mail template's, so wanted to offer a little additional information, to see if a feature already baked into the .net framework may offer an alternative solution. To clarify, the separate files isn't the added convenience, but the various features in the MailDefinition class may prove convenient when sending e-mail in .net. – Alex KeySmith May 12 '11 at 14:43