Load HTML page to string

Aug 30, 2011 at 6: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 = "test@test.com";
        public string ToAddress = "test2@test2.com";
    
     

        
        public Form1()
        {
            InitializeComponent();
        }

        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");
            SQLConnection.Open();
            SQLCommand = new SqlCommand("select * from EmailTemplate where TemplateName = @TemplateName", SQLConnection);
            SQLCommand.Parameters.Add(TemplateName);
            SQLResult = SQLCommand.ExecuteReader();
            SQLResult.Read();


        }
        
        public void Email()
        {
            string FromAddress = "test@test.com";
            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);
            mail.To.Add(ToAddress);

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

            //send the message
            SmtpClient smtp = new SmtpClient("laexchb.la.cryobank.com");
            smtp.Send(mail);
        }

        

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

            MessageBox.Show(NewBody);
            SQLConnection.Close();

        }
    }
}

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!