Database Template Example

Keeping your email templates organised is easily done by using a database to store and update them. You can use any Database you can connect to using .NET we recommend MSSQL Server.

Using a template is an easy way to send automated emails. You may want to send each user a welcome email when they sign up but change the details in the email.

Before setting up Database templates please select an option below;

You can also specify placeholders in your template which can then be replaced with content later.

For example you may put "Dear {Username}" which would become "Dear Tim" once you've added your content.

New Table Example

Contact Form
To
From
Send Type


Step By Step

Step 1

First open a new query window on your database and run the following script CreateEmailTemplateTable.SQL. It just creates a new table to store the templates in.

Step 2

Add some data into the new database table EasyEmails_EmailTemplates.

Step 3

Next we need to add the connection string to our database. Below is ours from this example. Note, the name of the connection string must be EasyEmail.

<add name="EasyEmail" 
connectionString=
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\EasyEmailDatabase.mdf;
Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
            

Step 4

Next add the following code to the click event of your send button, or in the code where you want to send the email.

//Create the recipient
EasyEmailForMandrill.Recipient recipient =
new EasyEmailForMandrill.Recipient(txtTo.Text);

//Create a content list to replace the place holders in the template
List<EasyEmail.EmailContent> content =
    new List<EasyEmail.EmailContent>();

//Now we can add some content to the content list
content.Add(
    new EasyEmail.EmailContent("{Content1}",
    "This replaces the place holder that said {Content1} with this text.")
    );
content.Add(
    new EasyEmail.EmailContent("{Content2}",
    "This replaces the place holder that said {Content2} with this text.")
    );

//Next we get the template by it's Label
EasyEmail.DatabaseTemplateRequest databaseTemplateRequest =
    new EasyEmail.DatabaseTemplateRequest("SampleTemplate");

//We could also do this by it's ID
//EasyEmail.DatabaseTemplateRequest databaseTemplateRequest =
//new EasyEmail.DatabaseTemplateRequest(1);

//Now we create the EasyEmail object
EasyEmail.EmailMessage email =
    new EasyEmail.EmailMessage(
        txtTo.Text,
        txtFrom.Text,
        databaseTemplateRequest);

//Or For Mandrill
EasyEmailForMandrill.MandrillEmail email =
    new EasyEmailForMandrill.MandrillEmail(
        recipient,
        txtFrom.Text,
        databaseTemplateRequest);

//Next we attach the content
email.AddContent(content);

//We then send the email and get a EasyEmailResponse back
EasyEmail.EasyEmailResponse response = email.Send();

//Finally we display the EasyEmailResponse message
lblOut.Text = "Easy Email response : " + response.Message;
            

Completed!

That's all you need to do to send an email from a Database template with Easy Email For Mandrill

Existing Table Example

Contact Form
To
From
Send Type


Step By Step

Step 1

First you need to add a new connection string for your database, below is ours from this example. Note, the name of the connection string must be EasyEmail.

<add name="EasyEmail" 
connectionString=
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\EasyEmailDatabase.mdf;
Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
            

Step 2

Next add the following code to the click event of your send button, or in the code where you want to send the email.

The important thing to note here is that we supply our own query and then simply add the names of the columns we're keeping the data in.

//Create the recipient
EasyEmailForMandrill.Recipient recipient =
new EasyEmailForMandrill.Recipient(txtTo.Text);

//Create a content list to replace the place holders in the template
List<EasyEmail.EmailContent> content =
    new List<EasyEmail.EmailContent>();

//Now we can add some content to the content list
content.Add(
    new EasyEmail.EmailContent("{Content1}",
    "This replaces the place holder that said {Content1} with this text.")
    );
content.Add(
    new EasyEmail.EmailContent("{Content2}",
    "This replaces the place holder that said {Content2} with this text.")
    );

//Next we get the template by it's Label
EasyEmail.DatabaseTemplateRequest databaseTemplateRequest =
    new EasyEmail.DatabaseTemplateRequest(
"SELECT TOP 1 * FROM EasyEmail_EmailTemplates WHERE Label LIKE 'Sample%'",
        "HTMLMessage",
        "PlainTextMessage",
        "Subject"
        );

//Now we create the EasyEmail object
EasyEmail.EmailMessage email =
    new EasyEmail.EmailMessage(
        txtToExisting.Text,
        txtFromExisting.Text,
        databaseTemplateRequest);

        //Or For Mandrill
EasyEmailForMandrill.MandrillEmail email =
    new EasyEmailForMandrill.MandrillEmail(
        recipient,
        txtFromExisting.Text,
        databaseTemplateRequest);

//Next we attach the content
email.AddContent(content);

//We then send the email and get a EasyEmailResponse back
EasyEmail.EasyEmailResponse response = email.Send();

//Finally we display the EasyEmailResponse message
lblOutExisting.Text = "Easy Email response : " + response.Message;
            

Completed!

That's all you need to do to send an email from a custom Database template with Easy Email For Mandrill