Click here to Skip to main content
15,895,799 members

How To send Excel file to particular user at specific day and specific time

Atul Rokade asked:

Open original thread
I am create one application that application one file send to user at specific time and specific day export sql data into excel ,sending email that functions working fine but problems is that it cannot execute particular time and particular day I am paste here my code please help me out


C#
private void Form1_Load(object sender, EventArgs e)
        {

            if ((DateTime.Now.DayOfWeek== DayOfWeek.Saturday) &&(DateTime.Now.Hour==19) &&(DateTime.Now.Minute==15))
            {
                exportsqldatatoexcle();

                emailattachment();


            }
}

<pre>public void exportsqldatatoexcle()
        {
            string con1 = "Data Source=ADMIN\\SQLEXPRESS;Initial Catalog=PhysioCure; Integrated Security=true";
            SqlConnection connection = new SqlConnection(con1);
            connection.Open();
            sda = new SqlDataAdapter("select PationName,RegistrationDate,ContactNo,Age,Sex,Chief_Complain,Investigation_Result,PastHistoryAny,Physical_Examination,Medications,Prognosis,Electro_Therapy,Neuro_Rehabilitation,Ortho_Rehabilitation,Cardio_Pulmonery_Rehabilitation,Sports_Rehabilitation from Physio_cureTable order by RegistrationDate desc", con1);
            dt = new DataTable();
            sda.Fill(dt);

            DataColumnCollection dccollection = dt.Columns;

            Microsoft.Office.Interop.Excel.ApplicationClass excelapp = new Microsoft.Office.Interop.Excel.ApplicationClass();

            excelapp.Application.Workbooks.Add(Type.Missing);


            for (int i = 1; i < dt.Rows.Count + 1; i++)
            {

                for (int j = 1; j < dt.Columns.Count + 1; j++)
                {
                    if (i == 1)
                    {
                        excelapp.Cells[i, j] = dccollection[j - 1].ToString();

                    }
                    else
                    {
                        excelapp.Cells[i, j] = dt.Rows[i - 1][j - 1].ToString();

                    }

                }

            }

            excelapp.ActiveWorkbook.SaveCopyAs("E:\\Winform n console\\PhysioCure\\PhysioCure\\Patient_Details.xls");
            excelapp.ActiveWorkbook.Saved = true;
            object m = Type.Missing;
            excelapp.ActiveWorkbook.Close(m, m, m);
            excelapp.Quit();
            connection.Close();
            MessageBox.Show("Detail file successfully create!");

        }

        private void emailattachment()
        {
            //try
            //{
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("my email id");
                mail.To.Add("my friend email id");
                mail.Subject = "Sending you Patient_Details File - 1";
                mail.Body = "Please Check Mail With Attachment dear..... ";

                System.Net.Mail.Attachment attachment;
                attachment = new Attachment("E:\\Winform n console\\PhysioCure\\PhysioCure\\Patient_Details.xls");
                mail.Attachments.Add(attachment);

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("my email id", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

                MessageBox.Show("Details File send on your mail please check your mail");
            //}
            /*catch (Exception ex)
            {
                MessageBox.Show("Mail cannot send please contact developer!!");

            }*/


        }
Tags: ADO.NET, SQL Server 2005, SQL Server

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900