Sending Html emails in Asp.net using C#

In the previous example we have learnt how to send emails in Asp.net using C# and now in this example we will learn how to send html emails in Asp.net using C#.

The procedure is Quite same to the previous example just you needed to create a function that will return string to us, in example mentioned below that function is protected string Bodytext(). Secondly you have to call this function like the same way I am calling in the example given below msg.Body = Bodytext(); and then you have to mention this line msg.IsBodyHtml = true; in your C# code

Let’s have a look over the example that will demonstrate how to send the Html emails in asp.net using C#

Sending_Html_Email.aspx


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net; //Use for sending Emails
using System.Net.Mail;//Use for sending Emails


public partial class Examples_Sending_Html_Email : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btn_Submit_Click(object sender, EventArgs e)
{
sendEmail();
}

//Function to send Emails in ASP.NET using C#

protected string Bodytext()
{
string Text = "";
Text = "

" + txt_Message.Text + "

";
return Text;
}
public void sendEmail()
{
MailMessage msg = new MailMessage(txt_From.Text,txt_To.Text);
msg.CC.Add(txt_Cc.Text);
msg.Bcc.Add(txt_Bcc.Text);
msg.Subject = txt_Subject.Text;
msg.Body = Bodytext();
msg.Subject = txt_Subject.Text;
msg.IsBodyHtml = true;
SmtpClient yourSmptpclient = new SmtpClient("localhost");
try
{
yourSmptpclient.Send(msg);
}
catch(Exception Exp)
{
throw Exp;
}
}
}


So this is the way to send HTML Emails in Asp.net Using C#

1 comments:

  • Anonymous
     

    I found something similar to this but it was a static class. Is there a reason you chose to go this route?
    http://easykb.com/default.aspx?id=43