Send Email using c# with proccessing bar

In this,post i will explain how to send email using c# with proccessing bar.
To do this,you have to follow few below given step.
step 1. first of all create a form as shown below:-


step 2. After making a form,drag a openfiledailog tool so that you can give path to file that you want to send              to the email.

step 3. Double click the select button shown in image above and paste below given code.

  if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox6.Text = openFileDialog1.FileName;
            }

step 4. Now Double click the send email button and paste below given code.

 progressBar1.Visible = true;
            MailMessage Mail = new MailMessage();
            progressBar1.Value = 10;

            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            progressBar1.Value = 20;
            Mail.From = new MailAddress(textBox1.Text);
            progressBar1.Value = 25;
            Mail.To.Add(textBox3.Text);
            progressBar1.Value = 30;
            Mail.Subject = textBox4.Text;
            progressBar1.Value = 35;
            Mail.Body = textBox5.Text;
            progressBar1.Value = 40;
            System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(textBox6.Text);
            progressBar1.Value = 50;
            Mail.Attachments.Add(attachment);
            progressBar1.Value = 55;
          
       
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
            smtp.EnableSsl = true;
            smtp.Send(Mail);
            progressBar1.Value = 100;
            MessageBox.Show("mail send");
            progressBar1.Visible = false;

step 5. Now Run the program by ctrl+F5 or using menu bar(go to Debug<<start without debugging).
            and fill the given fields and click send email button.
Thank for reading this..
Enjoy code



How to send email using c# with attachment

In this post, I will tell how to send email using c#.

There are few steps that must be followed to make email client that can send email from gmail account to any of other email address. For this we will use a new class System.Net.Mail and use some of there method like MailMessage and SmtpClient.

Step 1. Make a form shown below:-



Step 2. In this. we will use a tool  Openfiledialog which will be used for selecting the file, so select openfile dialog  and drag it on the window form
Step 3. Now double click on select file button and paste the coding:-
 (Note:- before pasting this code add a referance to the class using System.Net.Mail;)

if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox6.Text = openFileDialog1.FileName;
            }
Step 4. Again come to design view and double click on the send email and paste the coding:-
  MailMessage Mail = new MailMessage();
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            Mail.From = new MailAddress(textBox1.Text);
            Mail.To.Add(textBox3.Text);
            Mail.Subject = textBox4.Text;
            Mail.Body = textBox5.Text;
            System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(textBox6.Text);
            Mail.Attachments.Add(attachment);
            System.Net.Mail.Attachment attachment1 = new System.Net.Mail.Attachment(textBox7.Text);
            Mail.Attachments.Add(attachment1);
      
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
            smtp.EnableSsl = true;
            smtp.Send(Mail);
            MessageBox.Show("mail send");

Now save your project and run it and fill detail to send email.
Download source code(click to download)


Thank for reading this..
Enjoy code.

Show Data Of Excel File In The DataGridView



In this post , we will tell you how to show the data of excel file in the DataGridView.

There are a few steps that should be followed to show the data in DataGridView. In this, we will use new class DataTable. Firstly we store data in datatable and than show in the DataGridView by datatable.
Basically datatable is class that can store data in tabular form.


Step 1. Open visual stdio and open Window Form Application.

Stpep2 . Add reference to Micosoft library 12.0 object library. (Read more)

Step 3. Make a new form and add a button and dataGridView in the form as shown below:-




Step 4.  Double click “Read Data From Excel  file” button and paste the coding given below:-

Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

            string str;
            int rowcount = 0;
            int colcount = 0;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open("create-Excel.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
         int   colcount1 = range.Columns.Count;
            string[] st = new string[colcount1 + 1];
  ////////////////////////////////////////////////////DATA STORING IN THE DATATABLE FROM EXCEL FILE ///////////////////////////
            DataTable dt = newDataTable();

            for (rowcount = 0; rowcount < range.Rows.Count; rowcount++)
            {
               
                for (colcount = 0; colcount < range.Columns.Count; colcount++)
                {
                    str = Convert.ToString((range.Cells[rowcount+1, colcount+1]).Value);

                    st[colcount] = str;
                    if (rowcount == 0)
                    {
                      
                        dt.Columns.Add(st[colcount], typeof(string));
                    }
    
                     
                }
                if(rowcount!=0)
                    dt.Rows.Add(st[0], st[1], st[2], st[3], st[4]);
              
            }
            ////////////////////////////////////////////////END OF THE SNIPPET////////////////

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            release(xlWorkSheet);
            release(xlWorkBook);
            release(xlApp);
            dataGridView1.DataSource = dt;



Step 5. Now make a new class named as release as shown below:-

  private void release(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
       


Step 6. Now run project  and click “read data From Excel File” button, Result will be shown as below:-






Note. A reference should be added (using Excel=Microsoft.Office.Interop.Excel)


Thanks for reading post
Enjoy.

Read Excel File Using c#



In this post we will tell you  how to read excel file using c#.

There are few step that should follow to read  a file. For this operation ,firstly we have to know about the area that has been used. For this purpose we will use “UsedRange”  property of the worksheet class. This will return last cell of the used area. And also most important method  “xlapp.workbook.open()  for opening the excel file.

Step 1. Open visual stdio and open Window Form Application.


Stpep2 . Add reference to Micosoft library 12.0 object library. (Read more)

Step 3. Make a new form and add a button in the form as shown below:-





Step 4. Double click read button and paste below given coding:-

Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

       string str;
            int rowcount = 0;
            int colcount = 0;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open("create-Excel.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;

            for (rowcount = 1; rowcount <= range.Rows.Count; rowcount++)
            {
                for (colcount = 1; colcount <= range.Columns.Count; colcount++)
                {
                    str =Convert.ToString((range.Cells[rowcount, colcount] as Excel.Range).Value2);
                    MessageBox.Show(str);
                }
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            release(xlWorkSheet);
            release(xlWorkBook);
            release(xlApp);



step 5. Now make a new class named as release as shown below:-

  private void release(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

Step 6. Now run project  and click read your file’s cell will read one by one.

Note. A reference should be added (usingExcel=Microsoft.Office.Interop.Excel)

Result will be shown as:-


 Thank for reading post.
Enjoy