How to send email in Asp .net C#

How to send email in Asp.net using c#. In Asp.net 2.0,email sending is very easy.In our websites we send a feedback,contact us,registration welcome,Forgot password, news letter etc. to the users mail.By using the Asp.net classes.
we can access to send the email.The Simple Mail Transfer Protocol(SMTP) server is used for deliver the mail
using the System.Net.Mail namespace.

In System.Net.Mail namespace having two sub classes MailMessage and SmptClient.

MailMessage contains the properties From,To,Subject,Body etc.

SmtpClient send the Mailmessage data to the SMTP server.

The SMTP server Store the emails in a queue.It sends the mail one after another.

The steps to send email:-
1.use the namespace.

using System;
using System.Net.Mail;
using System.Text;

2.use the MailMessage to send the mail with SMTP Connection Settings.

MailMessage mailmsg = new MailMessage("yourfromemail@yourdomain.in", "yourtoemail@domain.com");
SmtpClient smtpmail = new SmtpClient();

mailmsg.Subject = "Account information Verified";
StringBuilder bodyMessage= new StringBuilder();

bodyMessage.Append("<br>");
bodyMessage.AppendFormat("Response From your domain name");
bodyMessage.Append("<br>");
bodyMessage.AppendFormat("Your Account has been deleted if you are not activate\n\n");
bodyMessage.Append("<br>");
bodyMessage.AppendFormat("So Please follow the link To Activate Your Account with Us!\n\n");
bodyMessage.Append("Click Here To Activate Your Account");
bodyMessage.AppendFormat("Registered Email:", "yourtoemail@domain.com");
bodyMessage.AppendFormat("Note:- If U Already Activate your account means Please ignore this message");

mailmsg.Body = bodyMessage.ToString();
smtpmail.Host = "smtp.yourdomain.in";
smtpmail.Port = 587;
smtpmail.Send(mailmsg);

the mail will be send to the to address for activating the account information.

mailmsg.Subject is the subject of our mail.

mailmsg.Body contains the bodymsg string values.it attach the bodymsg with subject.

smtp.Host is your smtp server name for eg.

gmail smtp server is “smtp.gmail.com”.

smtpmail.port is the port by sending the mail.

smtpmail.Send function is used to concatenate your message,subject and send the mail through the port with the smtp host address.

Happy to send mail.

Advertisements

Dynamic LinkButton Controls in Asp.Net

In this article am going to explain how to create and add dynamic table with Dynamic LinkButton.

In the design page, we just add how many controls we need and where it should be placed. These controls are static and having the data what we gave. In some cases we have to dynamically get data from the database and create dynamic controls for the data.

Difference between static and dynamic?
Static -> we can mention how many things we need.
Dynamic ->we cannot mention it generates depends upon the requirements.

Creating Dynamic LinkButton in Asp.Net is very simple. After creating the dynamic controls just add the controls in a PlaceHolder or Panel control.

Step 1: In the Design Page just create a panel named “pnlDynamic”.

Step 2: Add the namespace in the code behind.

using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

Step 3: Create an object for HtmlTable.

HtmlTable tblDynamic = new HtmlTable();

Step 4: In the Page_Load event, just call the dynamically creating control method BindEmployeeData(); Call the method outside the IsPostBack() method. Because our dynamic controls should recreate each and every time when a postback call.

protected void Page_Load(object sender, EventArgs e)
  {
    If(!IsPostBack())
    {                  
    }
    BindEmployeeData ();
  }

Step 5: Method for creating the dynamic LinkButton control depends on the database data.

public void BindEmployeeData()
  {
      SqlConnection SqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=mytestDB;User ID=xxxxxxx;Password=zzzzzzzzz");
      SqlDataAdapter SqlDap = new SqlDataAdapter("select * from Employees", SqlConn);
      DataTable objDT = new DataTable();
      SqlDap.Fill(objDT);

      tblDynamic.ID = "tblEmployee";
      tblDynamic.Width = "100%";
      tblDynamic.CellPadding = 3;
      int intRowCount = objDT.Rows.Count;

      if ((intRowCount > 0))
      {
          int sCount = (intRowCount / 2) + 1;
          int intCurrentCount = 0;
          for (int i = 1; i <= sCount; i++)                 {                     HtmlTableRow tblRowDef = new HtmlTableRow();                                          tblRowDef.Align = "left";                                          if ((intRowCount > intCurrentCount))
              {
                  for (int l = 1; l <= 2; l++)                         {                             if ((intRowCount > intCurrentCount))
                      {
                          HtmlTableCell tblCellDef = new HtmlTableCell();
                          tblCellDef.ID = "0" + objDT.Rows[intCurrentCount]["EmpName"].ToString();
                          tblCellDef.Width = "400px";
                          LinkButton lnkEmployee = new LinkButton();
                          lnkEmployee.ID = "lnkEmployee0" + objDT.Rows[intCurrentCount]["EmpName"].ToString();
                          lnkEmployee.Text = objDT.Rows[intCurrentCount]["EmpName"].ToString();
                          lnkEmployee.CommandName = objDT.Rows[intCurrentCount]["EmpID"].ToString();
                          lnkEmployee.Font.Bold = true;
                          lnkEmployee.Font.Size = 13;
                          lnkEmployee.Width = 10;
                          lnkEmployee.Click += new EventHandler(lnkEmployeeWhole_Click);
                          tblCellDef.Controls.Add(lnkEmployee);
                          tblRowDef.Controls.Add(tblCellDef);
                          intCurrentCount += 1;
                      }
                  }
              }
              tblDynamic.Controls.Add(tblRowDef);
          }

      }
     pnlDynamic.Controls.Add(tblDynamic);
  }

Step 6: Write the handler to fire the event for the particular link button control

protected void lnkEmployeeWhole_Click(object sender, EventArgs e)
  {
      LinkButton lnkEmployee = (LinkButton)sender;
      Response.Redirect("SomePage.aspx?ID="+lnkEmployee.CommandName);
  }

Dynamic link button will disappear if you put LoadBookData() method within IspostBack() method.

I Hope these Code will help you to understand the Dynamic LinkButton Controls in Asp.Net.