public void DistributeReminder(bool sendCalendar = false) { var recipients = context.Users.Where(t => t.IsActive == true && t.IsDeleted != true && t.IsDraft == false && t.Email != null).Select(t => t.Email).ToArray(); if (recipients.Length <= 0) { return; } var date = DateTime.Today.Date; var reminders = GetReminders(date); var subject = $"App Reminders {date.ToString("dd MMMM yyyy", new System.Globalization.CultureInfo("id-ID"))}"; var body = CreateBody(reminders); ICSFileModel icsFileModel = new ICSFileModel() { Location = "Location", Subject = subject, Description = subject, StartDate = date.ChangeTime(16, 0, 0, 0), EndDate = date.ChangeTime(8, 0, 0, 0), IcsGuid = Guid.NewGuid().ToString(), Sequence = 1.ToString(), Method = "REQUEST" }; if (sendCalendar) { _emailSvc.SendEmailICS(recipients, subject, body, icsFileModel); } else { _emailSvc.SendEmailOnly(recipients, subject, body); } try { var log = new ActivityLog(); log.Action = "Distribute Reminder"; log.Url = ""; log.IPAddress = ""; log.HostName = ""; log.Modul = ""; log.Username = "******"; log.CreatedDate = DateTime.Now; log.Data = "Distribute Reminder " + DateTime.Now.ToString("yyyy MMM dd HH:mm:ss"); context.ActivityLogs.Add(log); context.SaveChanges(); } catch (Exception) { } }
public bool SendEmail(string[] email) { string subject = "Example Subject Message"; string body = "Example Body Message"; ICSFileModel icsFileModel = new ICSFileModel() { Location = "Derivative C", Subject = subject, Description = "Schedule Decription", StartDate = DateTime.Now, EndDate = DateTime.Now }; return(_service.SendEmailICS(email, subject, body, icsFileModel)); }
public void SendMail(string flowId) { string[] emails = null; var flow = context.Flows.FirstOrDefault(t => t.Id == flowId); if (flow?.DueDate == null) { return; } flow.EmailGuid = Guid.NewGuid().ToString(); flow.EmailSequence = 1; string subject = flow.Subject; string body = flow.Body; var roleManager = new RoleManager <ApplicationRole, string>(new RoleStore <ApplicationRole, string, IdentityUserRole>(context)); var role = roleManager.FindByName(flow.RoleRecipient); if (role != null) { var users = context.Users.Where(t => t.Roles.Any(r => r.RoleId == role.Id)).ToList(); if (users != null && users.Count > 0) { emails = users.Select(t => t.UserName).ToArray(); ICSFileModel icsFileModel = new ICSFileModel() { Location = "IDX", Subject = subject, Description = subject, StartDate = flow.DueDate?.ChangeTime(8, 0, 0, 0) ?? DateTime.MinValue, EndDate = flow.DueDate?.ChangeTime(16, 0, 0, 0) ?? DateTime.MinValue, IcsGuid = flow.EmailGuid, Sequence = 1.ToString(), Method = "REQUEST" }; _emailSvc.SendEmailICS(emails, subject, body, icsFileModel); flow.MailSent = true; context.SaveChanges(); } } }
public bool SendEmailWithGuid(string[] email, string guid, int sequence, string method = "REQUEST") { string subject = "Example Subject Message"; string body = "Example Body Message"; ICSFileModel icsFileModel = new ICSFileModel() { Location = "Derivative C", Subject = subject, Description = "Schedule Decription", StartDate = DateTime.Parse("2019-10-06"), EndDate = DateTime.Parse("2019-10-06"), IcsGuid = guid ?? Guid.NewGuid().ToString(), Sequence = (sequence).ToString(), Method = method }; return(_service.SendEmailICS(email, subject, body, icsFileModel)); }
public void CancelMail(Flow flow) { string[] emails = flow.Recipients?.Split(','); if (flow != null && emails != null && emails.Length > 0) { string subject = flow.Subject; string body = flow.Body; ICSFileModel icsFileModel = new ICSFileModel() { Location = "IDX", Subject = subject, Description = subject, StartDate = flow.DueDate ?? DateTime.MinValue, EndDate = flow.DueDate ?? DateTime.MinValue, IcsGuid = flow.EmailGuid, Sequence = ((flow.EmailSequence ?? 1) + 1).ToString(), Method = "CANCEL" }; _emailSvc.SendEmailICS(emails, subject, body, icsFileModel); } }
public bool SendEmailICS(string[] emailDestination, string subject, string body, ICSFileModel icsFileModel) { try { icsFileModel.IcsGuid = icsFileModel?.IcsGuid ?? Guid.NewGuid().ToString(); //Client Configuration for Send Email var client = new SmtpClient(WebAppSettings.SMTPClientHost, WebAppSettings.SMTPClientPort) { Credentials = new NetworkCredential(WebAppSettings.SMTPUsername, WebAppSettings.SMTPPassword), EnableSsl = WebAppSettings.UseSSL, Timeout = 100000, }; MailMessage msg = new MailMessage(); msg.From = new MailAddress("*****@*****.**"); msg.IsBodyHtml = true; foreach (var email in emailDestination) { try { msg.To.Add(new MailAddress(email?.Trim())); } catch (Exception exc) { ErrorLog(exc); } } msg.Subject = subject; //msg.Body = body; var htmlContentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html); var avHtmlBody = AlternateView.CreateAlternateViewFromString(body, htmlContentType); msg.AlternateViews.Add(avHtmlBody); // Now Contruct the ICS file using string builder StringBuilder str = new StringBuilder(); str.AppendLine("BEGIN:VCALENDAR"); str.AppendLine($"PRODID:-//{icsFileModel.Subject}"); str.AppendLine("VERSION:2.0"); str.AppendLine($"METHOD:{icsFileModel.Method??"REQUEST"}"); str.AppendLine("BEGIN:VEVENT"); str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", icsFileModel.StartDate)); str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow)); str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", icsFileModel.EndDate)); str.AppendLine("LOCATION: " + icsFileModel.Location); str.AppendLine(string.Format("UID:{0}", icsFileModel.IcsGuid)); str.AppendLine(string.Format("DESCRIPTION:{0}", icsFileModel.Description)); str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", icsFileModel.Description)); str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject)); str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address)); str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address)); str.AppendLine(string.Format("SEQUENCE:{0}", icsFileModel.Sequence)); str.AppendLine(string.Format("X-MICROSOFT-CDO-APPT-SEQUENCE:{0}", icsFileModel.Sequence)); var stst = icsFileModel.Method == "CANCEL" ? "CANCELLED" : "TENTATIVE"; str.AppendLine($"X-MICROSOFT-CDO-BUSYSTATUS:{stst}"); str.AppendLine($"STATUS:{stst}"); str.AppendLine("BEGIN:VALARM"); str.AppendLine("TRIGGER:-PT15M"); str.AppendLine("ACTION:DISPLAY"); str.AppendLine("DESCRIPTION:Reminder"); str.AppendLine("END:VALARM"); str.AppendLine("END:VEVENT"); str.AppendLine("END:VCALENDAR"); System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar"); contype.Parameters.Add("method", icsFileModel.Method ?? "REQUEST"); contype.Parameters.Add("name", "Meeting.ics"); AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype); msg.AlternateViews.Add(avCal); msg.Headers.Add("Content-class", "urn:content-classes:calendarmessage"); msg.IsBodyHtml = true; //Send Email client.Send(msg); } catch (Exception excp) { ErrorLog(excp); return(false); } return(true); }