This project has moved and is read-only. For the latest updates, please go here.

Load HTML page to string

Aug 30, 2011 at 7:04 PM

Hi,  eveyrone.


I am working on a HTML Email merge project. Idea is very simple.  Create HTML email template, call some variable from sql server and insert variables into html email template.

Problem I am having is replacing special variable like '@LastName' in HTML email template with actual last name value from database.  I am not sure if HTMLAgilityPack can replace pre-specifed charters with data or not... if so how would do this?? I am a system admin with minimal programming background... and so far I wrote follwoing code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Data.SqlClient;

namespace Email_Application
    public partial class Form1 : Form
        SqlDataReader SQLResult = null;
        SqlCommand SQLCommand;
        SqlConnection SQLConnection;
        SqlParameter TemplateName;
        public string TemplateNameValue =  "Test";
        public string BranchEmail = "";
        public string ToAddress = "";

        public Form1()

        public void SQL()

            TemplateName = new SqlParameter("@TemplateName", SqlDbType.VarChar, 256);
            TemplateName.Value = TemplateNameValue;
            SQLConnection = new SqlConnection("server=local; database=EmailTemplate; User ID = SQLAccess; Password = SQLTEST");
            SQLCommand = new SqlCommand("select * from EmailTemplate where TemplateName = @TemplateName", SQLConnection);
            SQLResult = SQLCommand.ExecuteReader();

        public void Email()
            string FromAddress = "";
            string EmailContent = SQLResult["EmailBody"].ToString();
            string NewEmailContent = EmailContent.Replace("@BranchEmail", BranchEmail).Replace("@Email",ToAddress);
            //create the mail message
            MailMessage mail = new MailMessage();

            //set the addressesbhan c
            mail.From = new MailAddress(FromAddress);

            //set the content
            mail.Subject = SQLResult["EmailTitle"].ToString();
            mail.Body = NewEmailContent;
            mail.IsBodyHtml = true;

            //send the message
            SmtpClient smtp = new SmtpClient("");


        private void button1_Click(object sender, EventArgs e)
            string EmailBody = SQLResult["EmailBody"].ToString();
            string NewBody = EmailBody.Replace("@BranchEmail", BranchEmail);



So due to my limited C# programming skill, I am basically calling html code from sql server.  Ideally I want to save html email on our sharepoint (so user can create/edit html email with word), call template html email file, convert html email to string.

once html is converted to string, replace special word with database value and send html email to our client.  I know this is not the best, but with my 7 days of c# programming study, this was the best solution I came up with...


If you have any suggestion or help would be appricated!


Thank you!