private void _callSendEmailHoiVien() { using (QL_HOIVIEN_KTEntities context = new QL_HOIVIEN_KTEntities()) { WaitDialogForm _wait = new WaitDialogForm("Đang kiểm tra send mail ...", "Vui lòng đợi giây lát"); var query = (from p in context.QL_HOIVIEN let age = DateTime.Now.Year - p.HV_NGAY_SINH.Value.Year let monthBirth = p.HV_NGAY_SINH.Value.Month let dayBirth = p.HV_NGAY_SINH.Value.Day where (age == 16 || age == 60) && DateTime.Now.Month >= monthBirth && DateTime.Now.Day >= dayBirth && ((p.HV_SENDMAIL_16TUOI ?? false) == false || (p.HV_SENDMAIL_60TUOI ?? false) == false) select p); string messageHtml = ""; foreach (var item in query) { int age = DateTime.Now.Year - item.HV_NGAY_SINH.Value.Year; Boolean found = false; if (age == 16 && (item.HV_SENDMAIL_16TUOI ?? false) == false) { item.HV_SENDMAIL_16TUOI = true; found = true; } else if (age == 60 && (item.HV_SENDMAIL_60TUOI ?? false) == false) { item.HV_SENDMAIL_60TUOI = true; found = true; } if (found) { string hoten = item.HV_HO + " " + item.HV_TEN; messageHtml += string.Format("<p><b>{0}</b>-Ngày sinh:{1}({2} tuổi) - Địa chỉ:{3}</p>", hoten, item.HV_NGAY_SINH.Value.ToString("dd/MM/yyyy"), age.ToString(), item.HV_THUONGTRU_DIACHI); } } if (messageHtml != "") { context.SaveChanges(); clsMail mail = new clsMail(); mail.sendMail(messageHtml); } _wait.Close(); } }