private static void sendEmailSchedule() { using (SendMailEntities db = new SendMailEntities()) { List<TempScheduleSendEmail> list_temp = new List<TempScheduleSendEmail>(); List<LogSendEmail> lst_logEndEmail = new List<LogSendEmail>(); TempScheduleSendEmail latest = db.TempScheduleSendEmails.Where(x => x.IDUser == mGlobal.UserID).ToList().OrderBy(m => m.TimeSchedule).FirstOrDefault(); list_temp = db.TempScheduleSendEmails.Where(x => x.TimeSchedule == latest.TimeSchedule && x.IDUser == mGlobal.UserID).ToList(); Int64 idEmailOwn = Int64.Parse(list_temp[0].IDEmailOwn.ToString()); EmailOwn emailOwn = db.EmailOwns.FirstOrDefault(x => x.ID == idEmailOwn); foreach (var item in list_temp) { //get contact Contact contact = db.Contacts.FirstOrDefault(x => x.Email == item.Email); //save log send email LogSendEmail log = new LogSendEmail(); if (item.IDCampaign != null) { log.CampaignID = item.IDCampaign; } log.ContactID = contact.ContactID; log.StatusSend = true; log.IDEmailOwn = emailOwn.ID; log.TypeServiceUsed = "STPM"; log.UserID = mGlobal.UserID; log.Subject = item.Subject; log.Body = item.ContentEmail; log.TimeSend = (DateTime)item.TimeSchedule; lst_logEndEmail.Add(log); STPMService.SendMail(emailOwn.Email , Cryption.Decrypt(emailOwn.Password) , item.Email , item.Subject , item.ContentEmail); } //db.EmailContents.AddRange(lst_emailContent); db.LogSendEmails.AddRange(lst_logEndEmail); db.TempScheduleSendEmails.RemoveRange(list_temp); db.SaveChanges(); } }
protected void btnSendMail_Click(object sender, EventArgs e) { try { using (SendMailEntities db = new SendMailEntities()) { ListEditItem cmbEmailOwnselectedItem = cmbEmailOwn.SelectedItem; ListEditItem cmbcmbContactselectedItem = cmbContact.SelectedItem; ListEditItem cmbcmbCampaignselectedItem = cmbCampaign.SelectedItem; string toEmail = cmbcmbContactselectedItem.GetValue("Email").ToString(); string fromEmail = cmbEmailOwnselectedItem.GetValue("Email").ToString(); string password = Cryption.Decrypt(cmbEmailOwnselectedItem.GetValue("Password").ToString()); Int64 IdEmailOwn = Int64.Parse(cmbEmailOwnselectedItem.GetValue("ID").ToString()); //EmailOwn emailOwn = db.EmailOwns.FirstOrDefault(x => x.ID == IdEmailOwn); LogSendEmail email = new LogSendEmail(); email.Subject = ip_txt_subject.Value; email.Body = txt_content_mail.Value; email.ContactID = Int64.Parse(cmbcmbContactselectedItem.GetValue("ContactID").ToString()); email.UserID = mGlobal.UserID; email.IDEmailOwn = IdEmailOwn; email.CampaignID = Int64.Parse(cmbcmbCampaignselectedItem.GetValue("CampaignID").ToString()); if (radio_service_google.Checked) { email.TypeServiceUsed = mGlobal.GOOGLE; try { GoogleMailService.sendMail("*****@*****.**", createEmail.createMessage(email.Subject , email.Body , fromEmail , toEmail)); email.StatusSend = true; } catch (Exception) { email.StatusSend = false; } } else if (radio_service_stpm.Checked) { email.TypeServiceUsed = mGlobal.STPM; try { STPMService.SendMail(fromEmail , password , toEmail , email.Subject , email.Body); email.StatusSend = true; } catch (Exception) { email.StatusSend = false; } } email.TimeSend = DateTime.Now; db.LogSendEmails.Add(email); db.SaveChanges(); string message = "Gửi email thành công"; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + message + "');", true); } } catch (Exception v_e) { ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + v_e + "');", true); } }
protected void btnSendListMail_Click(object sender, EventArgs e) { try { SendMailEntities db = new SendMailEntities(); //List<EmailContent> lst_emailContent = new List<EmailContent>(); List <LogSendEmail> lst_logEndEmail = new List <LogSendEmail>(); List <TempSendEmail> lst_TempSendEmail = new List <TempSendEmail>(); ListEditItem cmbEmailOwnselectedItem = cmbEmailOwn.SelectedItem; ListEditItem cmbcmbCampaignselectedItem = cmbCampaign.SelectedItem; Int64 IdEmailOwn = Int64.Parse(cmbEmailOwnselectedItem.GetValue("ID").ToString()); EmailOwn emailOwn = db.EmailOwns.FirstOrDefault(x => x.ID == IdEmailOwn); lst_TempSendEmail = db.TempSendEmails.Where(x => x.IDUser == mGlobal.UserID).ToList(); //email.fromEmail = cmbEmailOwn.Text; //email.passWordSendMail = Cryption.Decrypt(selectedItem.GetValue("Password").ToString()); if (radio_service_google.Checked) { ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "Hiện chưa hỗ trợ service này!" + "');", true); } else if (radio_service_stpm.Checked) { foreach (TempSendEmail item in lst_TempSendEmail) { //get contact Contact contact = db.Contacts.FirstOrDefault(x => x.Email == item.Email); //save log send email LogSendEmail log = new LogSendEmail(); log.CampaignID = Int64.Parse(cmbcmbCampaignselectedItem.GetValue("CampaignID").ToString()); log.ContactID = contact.ContactID; log.Subject = item.Subject; log.Body = item.ContentEmail; //log.EmailID = emailContent.EmailID; log.StatusSend = true; log.IDEmailOwn = IdEmailOwn; log.TypeServiceUsed = mGlobal.STPM; log.UserID = mGlobal.UserID; log.TimeSend = DateTime.Now; lst_logEndEmail.Add(log); STPMService.SendMail(emailOwn.Email , Cryption.Decrypt(emailOwn.Password) , item.Email , item.Subject , item.ContentEmail); } //db.EmailContents.AddRange(lst_emailContent); db.LogSendEmails.AddRange(lst_logEndEmail); db.SaveChanges(); ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "Gửi thành công!" + "');", true); } } catch (Exception v_e) { ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + v_e + "');", true); // Debugger.Log(1, "Send Mail", "Failed: " + v_e); } }