File/Image Updation or delete it using C#.Net , Asp.Net

 // FILE OR IMAGE DELETION AND CREATION ( Use for delete the Image) (UPDATING
// THE PREVIOUS FILE using c#.net , asp.net


using System.Drawing;
using System.IO;

public void filedel()        
    {
        string path = Server.MapPath(@"images\");
        string imgurl = path + "a.jpg";
        try
        {
            FileInfo TheFile = new FileInfo(imgurl);
            if (TheFile.Exists)
            {
                File.Delete(imgurl);   // It not works if file is used in another process
            }
            else
            {
               // File.Create(imgurl);
            //or
            // Code To Create image
            Bitmap bmp = new Bitmap(2, 2);
            bmp.Save(imgurl, System.Drawing.Imaging.ImageFormat.Jpeg);
            bmp.Dispose();
            }
            Label1.Text = "Sucess";
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }

Advance Email Sending(With Attachment) Code(C#.NET with ASP.NET) using gmail


 Advance Email Sending Option , with attachement.
Task: Sending Email with attachment using c# code in asp.net website/ web application.
Description: Some time we need to a website viewer can send a enquiry with some attachment as  some kind of career option where a candidate can submit his/her resume and the website administrator receive the same within email.

As in my previous post I discuss about a email notification http://hemantrautela.blogspot.com/2012/08/how-to-send-email-from-website-aspnet.html without attachment option , Now we require a email with attachment, I use below code for sending email with attachment . Here I use two fileupload control for uploading attachment file (here I am not checking the extension of file / as you can check it first before sending email ).

// Below Function can be used for sending a email with attachment option using c# code
//  using gmail crdential
using System.Net.Mail;
public bool mailattachement2(string to, string replyto, string body, string subject, FileUpload f1, FileUpload f2)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.From = new MailAddress("xyz@gmail.com","Client Enquiry");
        mail.Subject = subject;
        mail.Body = body;
        MailAddress rt = new MailAddress(replyto);
        mail.ReplyTo = rt;
        //mail.Sender = rt;
        mail.IsBodyHtml = true;
        if (f1.HasFile)
        {
            mail.Attachments.Add(new Attachment(f1.PostedFile.InputStream, f1.FileName)); //add the attachment
        }
        if (f2.HasFile)
        {
            mail.Attachments.Add(new Attachment(f2.PostedFile.InputStream, f2.FileName)); //add the attachment
        }

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "password");
// Set the one email id and its password for authentication )
// email goes via using above email id...

        smtp.Port = 587;
        smtp.EnableSsl = true;
        try
        {
            smtp.Send(mail);
            return (true);
        }
        catch (Exception ex)
        {
            return (false);
        }
    }

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);
        }
    }