From my expected, GlobalRescource .resx file is obviously handy to store email templates.
You write whatever you want that Html email template or text email template in eg(contorlsculture.resx) under App_GlobalResources.
And follows following codes -
private string FormatEmailBody(StringBuilder emailBody)
{
emailBody.Replace("{jobtitle}", HttpUtility.HtmlEncode(drVacancy.Title));
emailBody.Replace("{reference}", HttpUtility.HtmlEncode(drVacancy.Reference));
emailBody.Replace("{location}", HttpUtility.HtmlEncode(drVacancy.Location));
emailBody.Replace("{salary}", HttpUtility.HtmlEncode(drVacancy.Salary));
emailBody.Replace("{title}", HttpUtility.HtmlEncode(txtTitle.Text));
emailBody.Replace("{firstname}", HttpUtility.HtmlEncode(txtFirstName.Text));
emailBody.Replace("{surname}", HttpUtility.HtmlEncode(txtSurname.Text));
emailBody.Replace("{email}", HttpUtility.HtmlEncode(txtEmail.Text));
emailBody.Replace("{telephone}", HttpUtility.HtmlEncode(txtTelephone.Text));
emailBody.Replace("{message}", HttpUtility.HtmlEncode(txtMessage.Text));
return emailBody.ToString();
}
private void SendEmail()
{
// Sends Job application to recruiter
string strEmailBody = FormatEmailBody(new StringBuilder(Resources.ControlsCulture.JobApplicationTemplate));
EmailHelper.SendMail(drVacancy.Title + " Job - Application", strEmailBody, new string[] { ConfigHelper.RecruitmentEmail }, null, null,
ConfigHelper.NoReplyEmail.ToString(), fuApplicationForm.PostedFile, fuCV.PostedFile, fuOpsForm.PostedFile);
txtTitle.Text = txtFirstName.Text = txtSurname.Text = txtEmail.Text = txtConfirmEmail.Text =
txtTelephone.Text = txtMessage.Text = "";
phMessage.Visible = true;
}
protected void lkbSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
SendEmail();
}
}
That takes you get sucessful email sending process.
But on the other hand, some opposite idea programmer wants to use a physical files for email templates.
Then creates template files under App_data folder.
now a little bit needs to update the code of sending email. Basically, uses IO and stringbuilder class.
Something like that :
private string FormatEmailBody(StringBuilder emailBody)
{
emailBody.Replace("{contact}", HttpUtility.HtmlEncode("Administrator"));
emailBody.Replace("{name}", HttpUtility.HtmlEncode(txtTitle.Text + " " + txtFirstName.Text + " "
+ txtSurname.Text));
emailBody.Replace("{email}", HttpUtility.HtmlEncode(txtEmail.Text));
emailBody.Replace("{housename}", HttpUtility.HtmlEncode(txtHouseName.Text));
emailBody.Replace("{street}", HttpUtility.HtmlEncode(txtNumberStreet.Text));
emailBody.Replace("{area}", HttpUtility.HtmlEncode(txtLocation.Text));
emailBody.Replace("{town}", HttpUtility.HtmlEncode(txtCity.Text));
emailBody.Replace("{county}", HttpUtility.HtmlEncode(txtCounty.Text));
emailBody.Replace("{postcode}", HttpUtility.HtmlEncode(txtPostcode.Text));
emailBody.Replace("{telephone}", HttpUtility.HtmlEncode(txtTelephone.Text));
emailBody.Replace("{enquiry}", HttpUtility.HtmlEncode(txtEnquiry.Text));
return emailBody.ToString();
}
private void SendEmail()
{
string sFullTemplatePath = HttpContext.Current.Server.MapPath(drFormEmails.EmailTxtFileName);
StringBuilder sbTemplate = new StringBuilder();
using (StreamReader sr = new StreamReader(sFullTemplatePath))
{
sbTemplate = new StringBuilder(sr.ReadToEnd());
}
string strEmailBody = FormatEmailBody(sbTemplate);
EmailHelper.SendMail("Contact us", strEmailBody, new string[] { drFormEmails.MailTo }, null, null,
drFormEmails.MailFrom);
}
protected void lbSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
SendEmail();
Response.Redirect("/Contact-us-confirmation");
}
}
Any better suggestion, please comment it. I will reply you.
No comments:
Post a Comment