Exemple #1
0
        private void auto_worker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                EmailSend mailSend = new EmailSend();
                DataTable tableSend = sendBUS.GetByTime(DateTime.Now, 0);
                if (tableSend.Rows.Count > 0)
                {
                    //lblStatus.Text = "Đang gửi mail....";
                    foreach (DataRow item in tableSend.Rows)
                    {
                        sendRegisterId = int.Parse(item["Id"].ToString());
                        sendContentId = int.Parse(item["SendContentId"].ToString());
                        configId = int.Parse(item["MailConfigID"].ToString());
                        groupId = int.Parse(item["GroupTo"].ToString());
                        SendType = int.Parse(item["SendType"].ToString());
                        accountId = int.Parse(item["AccountId"].ToString());

                        int SendRegisterId = int.Parse(item["Id"] + "");

                        //Lấy thông tin cấu hình mail gửi
                        getConfigServer(configId);

                        // Lấy nội dung mail
                        //getMailContent(sendContentId);
                        subject = item["Subject"].ToString();
                        body = item["Body"].ToString();

                        IList<MailToDTO> recipients = mailSend.GetMailTod(SendRegisterId, groupId, SendType);

                        //IList<string> EmailS
                        int cnt = 0;
                        int totalCnt = recipients.Count;
                        Parallel.ForEach(recipients.AsParallel(), new ParallelOptions { MaxDegreeOfParallelism = maxParallelEmails }, recipient =>
                        {
                            bool send = false;
                            //DataTable tblPartSend = psBus.GetByUserIdAndGroupId(accountId, groupId);
                            // Send normal.
                            send = sendMail(send, port, hostName, userSmtp, passSmtp, mailFrom, senderName,
                                subject, body, recipient.Name, recipient.MailTo, sendRegisterId, 0);
                            logs_info.Info("Status: " + send + ", sendRegisterId:" + sendRegisterId + ", MailTo: " + recipient.MailTo + ", mailFrom: " + mailFrom + ", Name: " + recipient.Name);
                            // Write log for history send
                            logHistoryForSend(sendRegisterId, recipient.MailTo, mailFrom, recipient.Name, send);

                            lock (syncRoot) cnt++;
                        });

                        //foreach (MailToDTO recipient in recipients)
                        //{
                        //    bool send = false;
                        //    //DataTable tblPartSend = psBus.GetByUserIdAndGroupId(accountId, groupId);
                        //    // Send normal.
                        //    send = sendMail(send, port, hostName, userSmtp, passSmtp, mailFrom, senderName,
                        //        subject, body, recipient.Name, recipient.MailTo, sendRegisterId, 0);
                        //    logs_info.Info("Status: " + send + ", sendRegisterId:" + sendRegisterId + ", MailTo: " + recipient.MailTo + ", mailFrom: " + mailFrom + ", Name: " + recipient.Name);
                        //    // Write log for history send
                        //    logHistoryForSend(sendRegisterId, recipient.MailTo, mailFrom, recipient.Name, send);
                        //}

                        // Update status for send mail campaign.
                        sendBUS.tblSendRegister_UpdateStatus(sendRegisterId, 1, DateTime.Now);
                    }
                    //timer1.Start();
                    //lblStatus.Text = "Đang chờ gửi mail";
                }
            }
            catch (Exception ex)
            {
                logs.Error("auto_worker_DoWork", ex);

            }
        }