Beanie posted on September 6, 2009 16:22
Here we take the information from a contact form and email it to the site owner.

 

   1:  protected void btnSend_Click(object sender, EventArgs e)
   2:  {
   3:      if (Page.IsValid)
   4:      {
   5:          string fileName = Server.MapPath("ContactForm.txt");
   6:          string mailBody = string.Empty;
   7:          if (Cache["ContactFormMailBody"] == null)
   8:          {
   9:              mailBody = System.IO.File.ReadAllText(fileName);
  10:              Cache.Insert("ContactFormMailBody", mailBody, new CacheDependency(fileName));
  11:          }
  12:          else
  13:          {
  14:              mailBody = Cache["ContactFormMailBody"].ToString() + "\r\n(File from the cache)";
  15:          }
  16:   
  17:          mailBody = mailBody.Replace("##Name##", txtName.Text);
  18:          mailBody = mailBody.Replace("##Email##", txtEmailAddress.Text);
  19:          mailBody = mailBody.Replace("##HomePhone##", txtPhoneHome.Text);
  20:          mailBody = mailBody.Replace("##BusinessPhone##", txtPhoneBusiness.Text);
  21:          mailBody = mailBody.Replace("##Comments##", txtComments.Text);
  22:   
  23:          MailMessage myMessage = new MailMessage();
  24:          myMessage.Subject = "Response from web site";
  25:          myMessage.Body = mailBody;
  26:   
  27:          myMessage.From = new MailAddress("email@doamin.ext");
  28:          myMessage.To.Add(new MailAddress("web@doamin.ext"));
  29:          lblMessage.Text = "Your message has been sent to us";
  30:          SmtpClient mySmtpClient = new SmtpClient();
  31:          try
  32:          {
  33:              mySmtpClient.Send(myMessage);            
  34:          }
  35:          catch (Exception )
  36:          {
  37:              lblMessage.Text = "An error occurred while sending your e-mail. Please try again.";
  38:          }
  39:   
  40:          lblMessage.Visible = true;
  41:          FormTable.Visible = false;
  42:          System.Threading.Thread.Sleep(5000);
  43:      }
  44:  }
  45:   

The template

    Hi there,
A user has left the following feedback at the site:
Name:               ##Name##
E-mail address:     ##Email##
Home phone:         ##HomePhone##
Business phone:     ##BusinessPhone##
Comments:           ##Comments##

 


Posted in: Development , Development - ASP  Tags: , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Page List

Search Blog

Tag Cloud

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Beanie