Dynamic SEO for c# website,How To Set Html Title, Meta Keywords & Meta Description From Asp.Net(C#.Net)

Task: Put SEO stuff (Title of page, Meta keyword, Meta description) in website using c# code in asp.net website/ web application. Onsite SEO –in Dynamic Website or E-Commerce Website.

Description: Now these day SEO is a very important thing in a Website (Dynamic Website or E-Commerce Website). When we work on SEO for website then we require the setup page title, Meta keyword, Meta description. So we require option for this in admin panel for Content Management System Website.

Here I use a small function for this, you can connect it to database and call on pageload.

C# Code for Dynamic SEO

  public void settitle(string pagetitle, string metakeyword, string metadescription)
    {
        HtmlTitle obj = new HtmlTitle();
        obj.Text = pagetitle;
        Header.Controls.Add(obj);

        //  or title can be set as
        Page.Title = pagetitle;

        HtmlMeta mt = new HtmlMeta();
        mt.Name = "keywords";
        mt.Content = metakeyword;
        Header.Controls.Add(mt);

        //  or can be set as, for position of meta tag
        Header.Controls.AddAt(2,mt);

        HtmlMeta mtd = new HtmlMeta();
        mtd.Name = "description";
        mtd.Content = metadescription;
        Header.Controls.Add(mtd);

       //  or can be set as, for position of meta tag
        Header.Controls.AddAt(2,mtd);
    }

How to Send Email From Website Asp.Net C#.Net


 How to Send Email from website
Task: Send Email Notification using c# code in asp.net website/web application.
Description: Many times we want to send auto email to our website viewer or website owner. As we require after the viewer of website submits any enquiry then it should be come immediate in email Inbox Or In E-Commerce website after successfully purchasing any item a auto email notification will be send to customer (billing detail) and as well as website administrator also.
So that I use two variation of code for sending email as shown below. For this we require a email account and password for authentication.

First one is require your any one email account, password and mail server detail. The second one is for using gmail account.


using System.Web.Mail;
 public bool mailsend(string sSubject, string sBody, string sMailFrom, string sToMail)
    {
        try
        {
            MailMessage sMail = new System.Web.Mail.MailMessage();
            sMail.To = sToMail.ToString();
            sMail.From = sMailFrom.ToString();
            sMail.Subject = sSubject.ToString();
            sMail.Body = sBody.ToString();
            sMail.Priority = MailPriority.High;
            sMail.BodyFormat = MailFormat.Html;
            SmtpMail.SmtpServer = "mail.website.com"; // your mail server name

          sMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");  //basic authentication
          sMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "emailid");  //set your username here
          sMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");  //set your password here
            SmtpMail.Send(sMail);
            return true;
        }
        catch (Exception ex)
        {
           return false;
        }
    }

// using by gmail/smtp email id for authentication
 public bool mailsend(string to, string sub, string msg, string replyto)
    {
        string from = "yourmailid@gmail.com"; //Replace this with your own correct Gmail/Email Address
        string pwd = "password"; // Replace this with your above email id password.
        bool status = false;
        System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
        email.To.Add(to);
        email.From = new System.Net.Mail.MailAddress(to, "Client", System.Text.Encoding.UTF8);
        string mailbody = msg;
        email.Subject = sub;

        email.ReplyTo = new System.Net.Mail.MailAddress(replyto,"", System.Text.Encoding.UTF8);

        email.SubjectEncoding = System.Text.Encoding.UTF8;
        email.Body = mailbody;
        email.BodyEncoding = System.Text.Encoding.UTF8;
        email.IsBodyHtml = true;
        System.Net.Mail.SmtpClient emailclient = new System.Net.Mail.SmtpClient();
        emailclient.Credentials = new System.Net.NetworkCredential(from, pwd);
        emailclient.Port = 587; // Gmail works on this port
        emailclient.Host = "smtp.gmail.com"; // Your SMTP MailServer
        emailclient.EnableSsl = true; //Gmail works on Server Secured Layer
        try
        {
            emailclient.Send(email);
            status = true;
            return (status);
        }
        catch (Exception ex)
        {
            status = false;
            return (status);
        }
    }

On Button Click How Open A page in new window in asp.net - c# code


Task: Open a new window using c# code in asp.net website/web application. 
Description: The problem is this we can use Response.Redirect() method for redirect/open  new webpage in same window, But how we can open a new page in new window.

So that I use javascript code(alert box) for popup new window in c# code behind file as describe below. I write below code on Button Click Event.

// Dynamic Alert box javascript using c# code in asp.net

 protected void btngeneraterecipt_Click(object sender, EventArgs e)
    {
        try
        {
            string queryString = "billformat.aspx";
            string jquery = "window.open('" + queryString + "');";
            ScriptManager.RegisterStartupScript(this,this.GetType(), "pop", jquery, true);
        }
        catch (Exception ee)
        {
            Label1.Text = ee.Message;
        }
    }

Image Upload & Resize in asp.net with c#.net

         Image Upload & Resize in asp.net
Task: Upload Image & Resize it proportionally.
Description: Now days we require upload product images in a E-Commerce Website & we want to resize it also. So we can resize image when uploaded by using admin panel.

Here I use below code to resize image with its proportionally. So it will not stretch. This code is useful for jpg and png images.

using System.Drawing;
 protected void button_upload_image(object sender, EventArgs e)
    {
       if (FileUpload1.PostedFile.ContentLength != 0)
       {
           string temp = Server.MapPath(@"images\");
           string path = temp + "hemant.jpg";
           temp = temp + "temp.jpg";
           FileUpload1.SaveAs(temp);
           setimagesize(temp, path, 800, 600,400,300);
        } 
    }



// Call this function for resizing ( compress height and width proportionally )
// destination and imagepath must be differ
 protected void setimagesize(string imagepath, string destination, int maxwidth, int maxheight,int minwid,int minhight)
    {
        string path = imagepath.ToString();
        string dest = destination.ToString();

        System.Drawing.Image img1 = System.Drawing.Image.FromFile(path); // source of large image
        int owd = Convert.ToInt32(img1.Width);
        int oht = Convert.ToInt32(img1.Height);
        //       Compress Large Image Height/Width to according our canvas max(800*600)
        int nwd, nht;
        nwd = owd;
        nht = oht;
        if (owd > oht)
        {
            if (owd > maxwidth)                                // For Maximum width
            {
                nwd = maxwidth;
                nht = oht * maxwidth / owd;
            }
            else if (owd < minwid)                      // For minimum width
            {
                nwd = minwid;
                nht = oht * minwid / owd;
            }
        }
        else
        {
            if (oht > maxheight)                                // For Maximum height
            {
                nht = maxheight;
                nwd = owd * maxheight / oht;
            }
            else if (oht < minhight)                    //  For Minimum width
            {
                nht = minhight;
                nwd = owd * minhight / oht;
            }
        }

        ///////////////////////////
        //     Saving Compress Image
        ///////////////////////////////////////
        Bitmap bmp = new Bitmap(nwd, nht);
        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, nwd, nht);
        gr.DrawImage(img1, rectDestination, 0, 0, owd, oht, GraphicsUnit.Pixel);
        bmp.Save(dest, img1.RawFormat); //image size reduce by using this code changing by    
        bmp.Save(dest);
        bmp.Dispose();
       gr.Dispose();
        img1.Dispose();
    }