private async Task <EmailSchedule> MapToEmailSchadle(EmailScheduleViewModel viewModel) { EmailSchedule entity = new EmailSchedule(); if (viewModel.SendingNow) { entity.SendDateTime = DateTime.Now; } else { entity.SendDateTime = viewModel.SendDate.Value.Date + viewModel.SendTime.Value.TimeOfDay; } if (viewModel.UseMyEmail) { entity.FromEmail = await GetUserInfo(u => u.Email); } else { entity.FromEmail = viewModel.FromEmail; } entity.EmailBody = viewModel.EmailBody; entity.EmailSubject = viewModel.EmailSubject; entity.Recipients = viewModel.Recipients; entity.UserId = await GetUserInfo(u => u.Id); return(entity); }
private bool EmailScheduleISDuplicate(EmailSchedule entity) { DateTime selectedDate = entity.SendDateTime.Date; DateTime start = new DateTime(entity.SendDateTime.Year, entity.SendDateTime.Month, entity.SendDateTime.Day, 0, 0, 0, 0, DateTimeKind.Unspecified); DateTime end = new DateTime(entity.SendDateTime.Year, entity.SendDateTime.Month, entity.SendDateTime.Day, 23, 59, 59, 0, DateTimeKind.Unspecified); return(db.EmailSchedules.Count(e => e.EmailSubject.Trim().ToLower() == entity.EmailSubject.Trim().ToLower() && e.SendDateTime > start && e.SendDateTime < end) > 0); }
public async Task <ActionResult> Create(EmailScheduleViewModel emailScheduleViewModel) { byte[] recipients = null; ViewBag.UserEmail = await GetUserInfo(u => u.Email); if (Request.Files.Count == 0 || Path.GetExtension(Request.Files[0].FileName).ToLower() != ".csv") { ModelState.AddModelError("Recipients", "Choose a valid CSV file."); } else { using (var ms = new MemoryStream()) { Request.Files[0].InputStream.CopyTo(ms); recipients = ms.ToArray(); } var recipientsString = Encoding.UTF8.GetString(recipients).Replace("\r", "").Replace("\n", "").TrimEnd(','); var emails = recipientsString.Split(','); EmailAddressAttribute emailValidator = new EmailAddressAttribute(); foreach (var email in emails) { if (!emailValidator.IsValid(email)) { ModelState.AddModelError("Recipients", email + " is not an email. The format of file must be like this: [email protected], [email protected], [email protected][,]"); break; } } } if (ModelState.IsValid) { emailScheduleViewModel.Recipients = recipients; EmailSchedule entity = await MapToEmailSchadle(emailScheduleViewModel); if (!EmailScheduleISDuplicate(entity)) { db.EmailSchedules.Add(entity); db.SaveChanges(); try { entity.JobId = BackgroundJob.Schedule(() => sendEmail(entity.Id), new DateTimeOffset(entity.SendDateTime)); } catch { db.EmailSchedules.Remove(entity); } finally { db.SaveChanges(); } return(RedirectToAction("Index", new { message = "Sending Email was scheduled on " + entity.SendDateTime.ToString() })); } ModelState.AddModelError("", "You can not send a same massage in one day."); } return(View(emailScheduleViewModel)); }
private void emailsetupformclick(object sender, EventArgs e) { EmailSchedule emailform = new EmailSchedule(); emailform.Show(); }
partial void DeleteEmailSchedule(EmailSchedule instance);
partial void UpdateEmailSchedule(EmailSchedule instance);
partial void InsertEmailSchedule(EmailSchedule instance);