Email settings config example

These keys have to be accessible by the DLL. Most likley in the base project that will be compiled.

 <appSettings>
    <add key="EmailTestMode" value="false" />
    <add key="EmailTestTo" value="test@testserver.com" />

    <add key="EmailSMTP" value="192.168.0.1" />
    <add key="EmailDefaultFrom" value="test@test.com" />
    <add key="EmailBCC" value="email@test.com;email2@another.com;more@emails.com" />
 <appSettings>

Generating Key Value Pairs for template merging

List<KeyValuePair<string, string>> variables = new List<KeyValuePair<string, string>>();
 variables.Add(new KeyValuePair<string, string>("$NAME$", _name));
 variables.Add(new KeyValuePair<string, string>("$EMAIL$", _email));
 variables.Add(new KeyValuePair<string, string>("$DATE$", string.Format("{0:dddd dd MMM yyyy hh:mm tt}",  Time.GetGMTNow())));

Then you call MergeTemplateAndVariables method, pass in the full path of the HTML file and the KeyPair list. This will take the HTML file and replace all the text placeholders $PLACEHOLDER$ and return HTML

Production example

public class Email
{
  public static bool SendReset(string email){
    string passwordResetToken = WebSecurity.GeneratePasswordResetToken(email, 60);

    //generate content from template email

    List<KeyValuePair<string, string>> variables = new List<KeyValuePair<string, string>>();
    variables.Add(new KeyValuePair<string, string>("$RESETTOKEN$", passwordResetToken));
    string body = KulaTools.Email.MergeTemplateAndVariables(HttpContext.Current.Request.PhysicalApplicationPath + "Emails\\PasswordReset.html", variables);

    var result = KulaTools.Email.Send(new KulaTools.Models.Email()
    {
      Subject = "Password reset request",
      From = "", //will use the webconfig - EmailDefaultFrom
      Recipient = email,
      Body = body,
      IsHtml = true
     });

    return result.HasErrors;
    }
}

Last edited Jun 21, 2013 at 8:54 AM by ppumkin, version 7

Comments

No comments yet.