Token replace from Db
<p>Actually 2 question one for tokens and for logic. </p>

<p>Login Page (user forgot there password) i am sending it through gmail for now and the mail arrive, My problem is how to achieve that:</p>

<p>suppose i have one table for now in db:</p>

<p>table1- email templates.</p>

<p>in the email tamplates table i have a mail : 'Forgot_Password'</p>

<p>that contains tokens like @@USER_NAME@@ @@PASSWORD@@ etc'.. my goal is to send it to user and that i have done so now i just need some function that receive as parameter the String emailBody that i have from the email. and change the tokens inside it to parameters value that i have in the cs page.
* when i'll have tokens also in the webpage it self i would like to make a table "Tokens"
and take the value from that table, how could i separate them from the one existing only in the code behind?
Can i make a function that will deal with all tokens scenarios ?
Is this is the right way of doing it? its seems a lot of repeating code, always connectionString
Sql commands for each one how do you simplify that maybe you can show me an example of classes or such
for dealing with that ? right now all my sql commands is like this for each scenario. </p>

<pre><code> var cmd = new SqlCommand("SELECT pass FROM users WHERE email LIKE '" + email_forgot.Value + "'", connection);
        connection.Open();

        var cmd_body =
            new SqlCommand("SELECT [TemplateValue] FROM [Templates] WHERE [TemplateName] LIKE 'forgot_password'", connection);
        var pass = ((string)cmd.ExecuteScalar());
        string emailBody = ((string)cmd_body.ExecuteScalar());

        var toAddr = email_forgot.Value;

        var sc = new SmtpClient
            {
                EnableSsl = true,
                Timeout = 30 * 1000
            };

        //AppSettings["EmailFrom"] contains sender name and address
        using (var msg = new MailMessage(ConfigurationManager.AppSettings["EmailFrom"], toAddr))
        {
            msg.Subject = "Password reminder";
            msg.IsBodyHtml = true;
            msg.Body = @emailBody;
            sc.Send(msg);
        }
    }
</code></pre>

<p>Appreciate any help. Thanks in advanced </p>
