Monday, June 1, 2009

Sending Templated Emails Using MailDefinition Object

There is a better way to format your email messages in ASP.NET using the MailDefinition object. It lets you to use an email template and define tokens which you want to replace in it. This helps keep the presentation and business layers clean & seperate and lets the designers go in and edit the email templates without having to navigate the StringBuilder jungle.

private void SendEmail(int customerID)

{
 
Customer customer = CustomerData.GetCustomer(customerID);

 MailDefinition mailDefinition = new MailDefinition();

 mailDefinition.BodyFileName = "~/Email-Templates/Order-Confirmation.html";

 mailDefinition.From = "no-reply@my-site.com";

//Create a key-value collection of all the tokens you want to replace in your template...

  ListDictionary ldReplacements = new ListDictionary();

 ldReplacements.Add("<%FirstName%>", customer.FirstName);

 ldReplacements.Add("<%LastName%>", customer.LastName);

 ldReplacements.Add("<%Address1%>", customer.Address1);

 ldReplacements.Add("<%Address2%>", customer.Address2);

 ldReplacements.Add("<%City%>", customer.City);

 ldReplacements.Add("<%State%>", customer.State);

 ldReplacements.Add("<%Zip%>", customer.Zip);

 string mailTo = string.Format("{0} {1} <{2}>", customer.FirstName, customer.LastName, customer.EmailAddress);

MailMessage mailMessage = mailDefinition.CreateMailMessage(mailTo, ldReplacements, this);

mailMessage.From = new MailAddress("no-reply@my-site.com", "My Site");

mailMessage.IsBodyHtml = true;

mailMessage.Subject = "Order Confirmation";

SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"].ToString(), 25);

smtpClient.Send(mailMessage);
}

Refrences:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.maildefinition.aspx

Thursday, May 21, 2009

HTTP handlers and HTTP Modules

Built-In HTTPHandlers:


































HttpHandler



Purpose



PageHandlerFactory



Processes .aspx pages.




WebServiceHandlerFactory




Processes .asmx XML Web services.




HttpForbiddenHandler




Yields an error message indicating that a type of page is not in service. By
default all .asax, .vb, .cs, .ascx, .config, .csproj, .vbproj, .webinfo files
are mapped to this in machine.config.




StaticFileHandler




Delivers any page that isn't specifically mapped, such as .html, .htm,
and .jpg.




TraceHandler




Shows the page containing all of the trace output.


Wednesday, May 20, 2009

To get membership database in SQL Server

Type the following in VSTS command prompt:
aspnet_regsql.exe -E -S -d -A all

Thursday, May 7, 2009

Sending special characters in Querystring

When you need to pass some special charaters(+, % etc.) in Querystring you will need to encode it first before redirecting.

Response.Redirect(www.abc.com/createtask.aspx?taskID= + Server.UrlEncode(Session["TaskGUID"].ToString())

This is very helpful when you are going to send some GUID values.
But keep in mind don't use Server.UrlDecode in createTask.aspx page as Request.Querystring automatically recieves decoded string.

Wednesday, May 6, 2009

Global.ASAX Error handler to know which file is missing

Write the following code in your Global Error handler to know which file is missing.
void Application_Error(object sender, EventArgs e)
{
String FileName = HttpContext.Current.Request.Url.ToString();
String RequestFrom = HttpContext.Current.Request.UrlReferrer.ToString();.....
}

Put a breakpoint at first line and check the value of FileName to know which file is missing.