Friday, October 19, 2012

How to send a Email with html Format in Asp.Net

In previous post we have seen how to send  a mail with attachement.Send a Email with html content can do in two ways.The first One is Creating html page as a body and Create html string as a body .Here i will show how to send the html format using String Builder in asp.net .In the below code i will send the user details to user in valid html format , who are submitting the form .This html string is bind to string builder then assign to email body.The below code have to place in button click event to send mail
Note:We need to set the ISbodyHtml to True For MailMessage properties
System.Text.StringBuilder mailBody = new System.Text.StringBuilder();
mailBody.Append("<b>Name:</b>");
mailBody.Append(Name.Value);
mailBody.Append("<br/>");
mailBody.Append("<b>Location:</b>");
mailBody.Append(Location.Value);
mailBody.Append("<br/>");
mailBody.Append("<b>Date:</b>");
mailBody.Append(Date.Value);
mailBody.Append("<br/>");
mailBody.Append("<b>Phone Number:</b>");
mailBody.Append(Phone.Value);
mailBody.Append("<br/>");
mailBody.Append("<b>City:</b>");
mailBody.Append(City.Value);
mailBody.Append("<br/>");
mailBody.Append("<b>State:</b>");
mailBody.Append(State.Value);
mailBody.Append("<br/>");
mailBody.Append("<b>Zip Code:</b>");
mailBody.Append(Zip.Value);
mailBody.Append("<br/>");
mailBody.Append("<b>Email Address:</b> ");
mailBody.Append(Email.Value);

///////////////////////////////////////Email Sending method//////////////////////////////////////////////////
string sFrom = Email.Value;
SmtpClient SmtpServer = new SmtpClient(SmtpserverName);
SmtpServer.Credentials = new System.Net.NetworkCredential(Username, PWD);
SmtpServer.Port = int.Parse(Port.ToString());
MailMessage objMailMsg = new MailMessage();
objMailMsg.From = new MailAddress("mulebhaskarareddy@gmail.com");
objMailMsg.Body = mailBody.ToString();
objMailMsg.IsBodyHtml = true;
objMailMsg.To.Add(SFrom);
objMailMsg.Subject = "Details of Registration";
SmtpServer.Send(objMailMsg);

 
Post a Comment

Bel