コード例 #1
0
        static void Main(string[] args)
        {
            try
            {
                ExcelHelper eh = new ExcelHelper();

                // do babylon
                ISTCDataDataSet1TableAdapters.BillOfLadingTableAdapter bol = new ISTCDataDataSet1TableAdapters.BillOfLadingTableAdapter();

                DateTime today      = DateTime.Now;
                DateTime reportDate = new DateTime(today.Year, today.Month, today.Day - 1);
                ISTCDataDataSet1.BillOfLadingDataTable table = bol.GetDataBPReport(25, reportDate);
                List <List <object> > writeRows = ExtractData(table);
                eh.WriteWorkSheet(writeRows, reportDate, "Babylon");

                // nj
                table     = bol.GetDataBPReport(59, reportDate);
                writeRows = ExtractData(table);
                eh.WriteWorkSheet(writeRows, reportDate, "Jersey");

                // bk
                table     = bol.GetDataBPReport(60, reportDate);
                writeRows = ExtractData(table);
                eh.WriteWorkSheet(writeRows, reportDate, "Brooklyn");


                string     path   = eh.SaveAs();
                Attachment attach = new Attachment(path);

                var          fromAddress  = new MailAddress("*****@*****.**", "ITC Tech");
                var          toAddress    = new MailAddress("*****@*****.**", "Dave");
                const string fromPassword = "******";
                const string subject      = "BP Email";
                const string body         = "See Attached";


                TextReader sr      = new StreamReader("d:/Reports/emails.txt");
                string     emailTo = sr.ReadLine();

                var smtp = new SmtpClient
                {
                    Host                  = "smtp.gmail.com",
                    Port                  = 587,
                    EnableSsl             = true,
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(fromAddress.Address, fromPassword)
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body
                })
                {
                    message.Attachments.Add(attach);
                    message.To.Add(emailTo);
                    smtp.Send(message);
                }
            }
            catch (Exception e)
            {
                TextWriter tw = new StreamWriter("D:/Reports/log.txt");

                // write a line of text to the file
                tw.WriteLine("ERRORS:");
                tw.WriteLine(e.Message + " " + e.StackTrace);

                tw.Close();
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            try
            {
                ExcelHelper eh = new ExcelHelper();

                // do babylon
                ISTCDataDataSet1TableAdapters.BillOfLadingTableAdapter bol = new ISTCDataDataSet1TableAdapters.BillOfLadingTableAdapter();

                DateTime today = DateTime.Now;
                DateTime reportDate = new DateTime(today.Year, today.Month, today.Day - 1);
                ISTCDataDataSet1.BillOfLadingDataTable table = bol.GetDataBPReport(25, reportDate);
                List<List<object>> writeRows = ExtractData(table);
                eh.WriteWorkSheet(writeRows, reportDate, "Babylon");

                // nj
                table = bol.GetDataBPReport(59, reportDate);
                writeRows = ExtractData(table);
                eh.WriteWorkSheet(writeRows, reportDate, "Jersey");

                // bk
                table = bol.GetDataBPReport(60, reportDate);
                writeRows = ExtractData(table);
                eh.WriteWorkSheet(writeRows, reportDate, "Brooklyn");

                string path = eh.SaveAs();
                Attachment attach = new Attachment(path);

                var fromAddress = new MailAddress("*****@*****.**", "ITC Tech");
                var toAddress = new MailAddress("*****@*****.**", "Dave");
                const string fromPassword = "******";
                const string subject = "BP Email";
                const string body = "See Attached";

                TextReader sr = new StreamReader("d:/Reports/emails.txt");
                string emailTo = sr.ReadLine();

                var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body

                })
                {
                    message.Attachments.Add(attach);
                    message.To.Add(emailTo);
                    smtp.Send(message);
                }
            }
            catch (Exception e)
            {
                TextWriter tw = new StreamWriter("D:/Reports/log.txt");

                // write a line of text to the file
                tw.WriteLine("ERRORS:");
                tw.WriteLine(e.Message + " " + e.StackTrace);

                tw.Close();
            }
        }