Ejemplo n.º 1
0
        private void btnGuiMail_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn muốn gửi mail ?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                List <string> vs = new List <string>();
                if ((txtFileDinhKem.EditValue ?? string.Empty) != string.Empty)
                {
                    vs = txtFileDinhKem.EditValue.ToString().Split(',').ToList();
                }

                O_CAUHINHSMTP cauHinhSMTPO = new D_CAUHINHSMTP().DuLieu();
                O_MAUEMAIL    ma           = new D_MAUEMAIL().DuLieu()[0];

                SmtpClient client = new SmtpClient();
                client.Port                  = cauHinhSMTPO.Port;
                client.Host                  = cauHinhSMTPO.Host;
                client.EnableSsl             = cauHinhSMTPO.SSL;
                client.Timeout               = 10000;
                client.DeliveryMethod        = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials           = new System.Net.NetworkCredential("*****@*****.**", "time0326");

                if (txtTen.Text.Length > 5)
                {
                    MailMessage mm = new MailMessage();
                    mm.From         = new MailAddress("*****@*****.**", "Thành Hoàng");
                    mm.BodyEncoding = UTF8Encoding.UTF8;
                    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                    mm.IsBodyHtml = true;
                    RichEditMailMessageExporter exporter = new RichEditMailMessageExporter(txtMauEmail, mm);
                    exporter.Export();
                    mm.To.Add(new MailAddress(txtTen.Text));
                    mm.Subject = txtTieuDe.Text;

                    foreach (string g in vs)
                    {
                        if (g.Count() > 0)
                        {
                            mm.Attachments.Add(new Attachment(g));
                        }
                    }
                    try
                    {
                        client.Send(mm);
                        XuLyGiaoDien.Alert("Gửi mail thành công", Form_Alert.enmType.Success);
                    }
                    catch (Exception ex) { XuLyGiaoDien.Alert(ex.Message, Form_Alert.enmType.Error); }
                }
            }
        }
Ejemplo n.º 2
0
        private void btnGuiMail_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn muốn gửi mail ?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                D_GIAODICH gdb = new D_GIAODICH();

                List <string> vs = new List <string>();
                if ((txtFileDinhKem.EditValue ?? string.Empty) != string.Empty)
                {
                    vs = txtFileDinhKem.EditValue.ToString().Split(',').ToList();
                }

                int n = lstDaiLy.CheckedItems.Count;
                if (n == 0)
                {
                    XuLyGiaoDien.Alert("Chưa chọn đại lý cần gửi", Form_Alert.enmType.Info);
                }
                else
                {
                    try
                    {
                        O_CAUHINHSMTP cauHinhSMTPO = cauHinhSMTPD.DuLieu();
                        O_MAUEMAIL    ma           = new D_MAUEMAIL().DuLieu()[0];

                        SmtpClient client = new SmtpClient();
                        client.Port                  = cauHinhSMTPO.Port;
                        client.Host                  = cauHinhSMTPO.Host;
                        client.EnableSsl             = cauHinhSMTPO.SSL;
                        client.Timeout               = 10000;
                        client.DeliveryMethod        = SmtpDeliveryMethod.Network;
                        client.UseDefaultCredentials = false;
                        client.Credentials           = new System.Net.NetworkCredential(cauHinhSMTPO.Email, cauHinhSMTPO.Password);

                        grvCTCongNo.Columns["Hang"].Width       = 50;
                        grvCTCongNo.Columns["SoVeVN"].Width     = 90;
                        grvCTCongNo.Columns["LuyKe"].Width      = 90;
                        grvCTCongNo.Columns["GiaThu"].Width     = 90;
                        grvCTCongNo.Columns["GiaHeThong"].Width = 90;
                        grvCTCongNo.Columns["TaiKhoanCo"].Width = 90;
                        grvCTCongNo.Columns["TenKhach"].Width   = 250;
                        grvCTCongNo.OptionsPrint.AutoWidth      = false;
                        grvCTCongNo.OptionsView.ColumnAutoWidth = false;

                        XlsxExportOptionsEx opt = new XlsxExportOptionsEx();
                        opt.CustomizeCell += op_CustomizeCell;
                        opt.SheetName      = "Bản Công Nợ";


                        bool sendOK = false;
                        if (!XuLyGiaoDien.wait.IsSplashFormVisible)
                        {
                            XuLyGiaoDien.wait.ShowWaitForm();
                        }
                        for (int i = 0; i < n; i++)
                        {
                            O_DAILY dl = lstDaiLy.GetItem(lstDaiLy.CheckedIndices[0]) as O_DAILY;
                            if (!dl.GuiMail)
                            {
                                int index1 = lstDaiLy.FindItem(0, true, delegate(ListBoxFindItemArgs ei) { ei.IsFound = object.Equals(dl.ID, ei.ItemValue); });
                                lstDaiLy.SetItemChecked(index1, false);
                                continue;
                            }
                            lstInt.Add(dl.ID);
                            string            daily     = string.Format("{0}", dl.ID);
                            List <O_GIAODICH> lstCongNo = gdb.LayDanhSachCN((DateTime)dtpTuNgay.EditValue, (DateTime)dtpDenNgay.EditValue, daily, true);
                            txtMauEmail.HtmlText = ma.NoiDung.Replace("{0}", dl.MaDL).Replace("{1}", XuLyDuLieu.NotVietKey(dl.Ten));

                            if ((dl.EmailKeToan ?? string.Empty) == string.Empty)
                            {
                                goto RE1;
                            }

                            string[] EmailKeToanString = Regex.Replace(dl.EmailKeToan, @"\t|\n|\r", "|").Replace("||", "|").Split('|');
                            for (int ii = 0; ii < EmailKeToanString.Count(); ii++)
                            {
                                if (EmailKeToanString[ii].Length > 5)
                                {
                                    MailMessage mm = new MailMessage();
                                    mm.From         = new MailAddress("*****@*****.**", "Thành Hoàng");
                                    mm.BodyEncoding = UTF8Encoding.UTF8;
                                    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                                    mm.IsBodyHtml = true;
                                    RichEditMailMessageExporter exporter = new RichEditMailMessageExporter(txtMauEmail, mm);
                                    exporter.Export();
                                    mm.To.Add(new MailAddress(EmailKeToanString[ii]));
                                    //mm.To.Add(new MailAddress("*****@*****.**"));
                                    if (lstCongNo.Count > 1)
                                    {
                                        XuLyGiaoDien.wait.SetWaitFormDescription("Gửi cho: " + dl.Ten + " (" + (i + 1) + "/" + n + ").");
                                        CTGiaoDichDindingSource.DataSource = lstCongNo;
                                        mm.Subject = "Công Nợ - " + dl.Ten + " - Từ ngày " + ((DateTime)dtpTuNgay.EditValue).ToString("dd_MM_yyyy") + " - đến ngày " + ((DateTime)dtpDenNgay.EditValue).ToString("dd_MM_yyyy");

                                        #region Xuất excel
                                        gridCTCongNo.ForceInitialize();
                                        string strFile = @"C:\CongNo\" + dl.Ten + " - " + ((DateTime)dtpTuNgay.EditValue).ToString("dd_MM_yyyy") + " - " + ((DateTime)dtpDenNgay.EditValue).ToString("dd_MM_yyyy") + ".xlsx";
                                        Directory.CreateDirectory(@"C:\CongNo");
                                        gridCTCongNo.ExportToXlsx(strFile, opt);
                                        #endregion

                                        mm.Attachments.Add(new Attachment(strFile));
                                        foreach (string g in vs)
                                        {
                                            if (g.Count() > 0)
                                            {
                                                mm.Attachments.Add(new Attachment(g));
                                            }
                                        }
                                        client.Send(mm);

                                        sendOK = true;
                                        mm.Attachments.Dispose();
                                        if (File.Exists(strFile))
                                        {
                                            File.Delete(strFile);
                                        }
                                    }
                                }
                            }
                            RE1 :
                            int index = lstDaiLy.FindItem(0, true, delegate(ListBoxFindItemArgs ei) { ei.IsFound = object.Equals(dl.ID, ei.ItemValue); });
                            lstDaiLy.SetItemChecked(index, false);
                        }

                        if (sendOK)
                        {
                            XuLyGiaoDien.Alert("Gửi mail thành công", Form_Alert.enmType.Success);
                        }
                        else
                        {
                            XuLyGiaoDien.Alert("Gửi mail không thành công", Form_Alert.enmType.Warning);
                        }
                    }
                    catch (Exception ex) { XtraMessageBox.Show(ex.Message, "Thông báo"); }
                    gdb.ChayCapNhatLuyKe(lstInt);
                    if (XuLyGiaoDien.wait.IsSplashFormVisible)
                    {
                        XuLyGiaoDien.wait.CloseWaitForm();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void btnGuiMail_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn muốn gửi mail ?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int n = lstDaiLyz.CheckedItems.Count;
                if (n == 0)
                {
                    XuLyGiaoDien.Alert("Chưa chọn đại lý tìm", Form_Alert.enmType.Info);
                }
                else
                {
                    O_CAUHINHSMTP cauHinhSMTPO = cauHinhSMTPD.DuLieu();
                    O_MAUEMAIL    ma           = new D_MAUEMAIL().DuLieu()[0];

                    SmtpClient client = new SmtpClient();
                    client.Port                  = cauHinhSMTPO.Port;
                    client.Host                  = cauHinhSMTPO.Host;
                    client.EnableSsl             = cauHinhSMTPO.SSL;
                    client.Timeout               = 10000;
                    client.DeliveryMethod        = SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = false;
                    client.Credentials           = new System.Net.NetworkCredential(cauHinhSMTPO.Email, cauHinhSMTPO.Password);


                    GCG.Visible = true; if (!XuLyGiaoDien.wait.IsSplashFormVisible)
                    {
                        XuLyGiaoDien.wait.ShowWaitForm();
                    }
                    List <long> a     = new List <long>();
                    string      daily = string.Format("{0}", lstDaiLyz.CheckedItems[0]);
                    a.Add(long.Parse(lstDaiLyz.CheckedItems[0].ToString()));
                    for (int i = 1; i < n; i++)
                    {
                        a.Add(long.Parse(lstDaiLyz.CheckedItems[i].ToString()));
                        daily += string.Format(",{0}", lstDaiLyz.CheckedItems[i]);
                    }

                    lst = new D_HOADON().DuLieu(string.Format("CONVERT(date, NgayThucHien) BETWEEN '{0}' AND '{1}' AND MaHD <> '0' AND ((GiaYeuCau - GiaHeThong) * PhanTram / 100) > 0 AND IDKhachHang in ({2}) ORDER BY IDKhachHang,MaHD,MaCho,GiaHeThong Desc", bdtpTu.DateTime.ToString("yyyyMMdd"), bdtpDen.DateTime.ToString("yyyyMMdd"), daily));

                    DevExpress.XtraPrinting.XlsxExportOptionsEx opt = new DevExpress.XtraPrinting.XlsxExportOptionsEx();
                    opt.CustomizeCell += op_CustomizeCell;
                    opt.SheetName      = "Bản CTHD";
                    opt.ApplyFormattingToEntireColumn = DefaultBoolean.False;
                    opt.ShowGridLines = false;

                    bool sendOK = false;
                    foreach (int b in a)
                    {
                        O_DAILY dl = lstDaiLy.Where(w => w.ID.Equals(b)).ToList()[0];
                        txtMauEmail.HtmlText = ma.NoiDung.Replace("{0}", dl.MaDL).Replace("{1}", XuLyDuLieu.NotVietKey(dl.Ten));
                        string[] EmailKeToanString = System.Text.RegularExpressions.Regex.Replace(dl.EmailKeToan, @"\t|\n|\r", "|").Replace("||", "|").Split('|');
                        for (int ii = 0; ii < EmailKeToanString.Count(); ii++)
                        {
                            if (EmailKeToanString[ii].Length > 5)
                            {
                                List <O_HOADON> lstTam1 = lst.Where(w => w.IDKhachHang.Equals(b)).OrderBy(w => w.MaHD.Replace(" ", string.Empty)).ToList();
                                List <O_HOADON> lstTam  = new List <O_HOADON>();
                                string          newrow  = string.Empty;
                                foreach (O_HOADON hd in lstTam1)
                                {
                                    if (newrow != hd.MaHD && newrow.Length > 0)
                                    {
                                        lstTam.Add(new O_HOADON());
                                    }
                                    lstTam.Add(hd);
                                    newrow = hd.MaHD;
                                }

                                lstTam.Add(new O_HOADON()
                                {
                                    ID         = -1,
                                    GiaHeThong = lstTam.Sum(w => w.GiaHeThong),
                                    GiaYeuCau  = lstTam.Sum(w => w.GiaYeuCau),
                                    CL1        = lstTam.Sum(w => w.CL1),
                                    CL2        = lstTam.Sum(w => w.CL2),
                                });

                                MailMessage mm = new MailMessage();
                                mm.From         = new MailAddress("*****@*****.**", "Thành Hoàng");
                                mm.BodyEncoding = UTF8Encoding.UTF8;
                                mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                                mm.IsBodyHtml = true;
                                RichEditMailMessageExporter exporter = new RichEditMailMessageExporter(txtMauEmail, mm);
                                exporter.Export();
                                mm.To.Add(new MailAddress(EmailKeToanString[ii]));
                                //mm.To.Add(new MailAddress("*****@*****.**"));

                                XuLyGiaoDien.wait.SetWaitFormDescription("Excel cho: " + dl.Ten);
                                guiOBindingSource.DataSource = lstTam;
                                string strFile = @"C:\HoaDon\CT HoaDon " + dl.Ten + ".xlsx";
                                System.IO.Directory.CreateDirectory(@"C:\HoaDon");
                                GVG.ExportToXlsx(strFile, opt);

                                mm.Attachments.Add(new Attachment(strFile));
                                mm.Subject = "Bảng kê hóa đơn Tháng " + bdtpTu.DateTime.Month + " - " + dl.Ten;
                                client.Send(mm);
                                sendOK = true;
                                mm.Attachments.Dispose();
                                if (File.Exists(strFile))
                                {
                                    File.Delete(strFile);
                                }
                            }
                        }
                    }
                    GCG.Visible = false;
                    if (XuLyGiaoDien.wait.IsSplashFormVisible)
                    {
                        XuLyGiaoDien.wait.CloseWaitForm();
                    }
                    if (sendOK)
                    {
                        XuLyGiaoDien.Alert("Gửi mail thành công", Form_Alert.enmType.Success);
                    }
                    else
                    {
                        XuLyGiaoDien.Alert("Gửi mail không thành công", Form_Alert.enmType.Warning);
                    }
                }
            }
        }