public ActionResult Delete(int id)
        {
            try
            {
                var viewModel = repository.EditGet(id);
                var res       = repository.DeleteVisitor(id);
                //mail notification

                TimeSpan     ts          = viewModel.EndTime - viewModel.StartTime;
                eAppointment appointment = new eAppointment();
                appointment.Subject  = "Visit Cancel Notification";
                appointment.Body     = "Your visit which has been scheduled on " + viewModel.visitDetails.StartTime.ToShortDateString() + " from " + viewModel.visitDetails.StartTime.ToShortTimeString() + " to " + viewModel.visitDetails.EndTime.ToShortTimeString() + " has been cancelled.";
                appointment.Location = "Canarys Automation";
                appointment.Duration = Convert.ToInt32(ts.TotalMinutes);
                DateTime dt       = viewModel.VisitingDate;
                TimeSpan timeSpan = new TimeSpan(viewModel.StartTime.Hour, viewModel.StartTime.Minute, viewModel.StartTime.Second);
                viewModel.VisitingDate = viewModel.VisitingDate.Add(timeSpan);
                appointment.Start      = Convert.ToDateTime(viewModel.VisitingDate);

                _context = new VisitorManagementContext();
                List <string> EmailIDs = new List <string>();

                if (viewModel.PrimaryParticipant != 0)
                {
                    string mailid = _context.User.Where(r => r.UserID == viewModel.PrimaryParticipant).FirstOrDefault().EmailAddress;
                    EmailIDs.Add(mailid);
                }
                if (viewModel.SecondaryParticipant != null && viewModel.SecondaryParticipant.Count > 0)
                {
                    for (int i = 0; viewModel.SecondaryParticipant.Count > i; i++)
                    {
                        string EmailAddres = _context.User.Where(r => r.UserID == viewModel.SecondaryParticipant[i]).FirstOrDefault().EmailAddress;
                        EmailIDs.Add(EmailAddres);
                    }
                }
                if (viewModel.VisitArrangement != null && viewModel.VisitArrangement.Where(r => r.IsSelected == true).Count() > 0)
                {
                    for (int i = 0; viewModel.VisitArrangement.Where(r => r.IsSelected == true).Count() > i; i++)
                    {
                        string mailid = _context.User.Where(r => r.UserID == viewModel.VisitArrangement.Where(l => l.IsSelected == true).ElementAt(i).DelegateContactID).FirstOrDefault().EmailAddress;
                        EmailIDs.Add(mailid);
                    }
                }
                EmailIDs.Add(viewModel.visit.EmailAddress);
                appointment.Email         = EmailIDs.ToArray();
                appointment.MeetingStatus = Microsoft.Office.Interop.Outlook.OlMeetingStatus.olMeeting;
                outLookEvents.OutLookEvent(appointment, true);
                return(Json(res));
            }
            catch (System.Exception)
            {
                //throw ex;
                return(Json(false));
            }
        }
Example #2
0
 public string OutLookEvent(eAppointment appointment, Boolean SendMail)
 {
     try
     {
         Application     outlookapp    = new Application();
         AppointmentItem agendaMeeting = (AppointmentItem)outlookapp.CreateItem(OlItemType.olAppointmentItem);
         if (agendaMeeting != null)
         {
             agendaMeeting.MeetingStatus = appointment.MeetingStatus;
             agendaMeeting.Location      = appointment.Location;
             agendaMeeting.Subject       = appointment.Subject;
             agendaMeeting.Body          = appointment.Body;
             agendaMeeting.Start         = appointment.Start;
             agendaMeeting.Duration      = appointment.Duration;
             // save the appointment
             agendaMeeting.Save();
             if (SendMail)
             {
                 Recipient recipient = null;
                 foreach (var mails in appointment.Email)
                 {
                     recipient = agendaMeeting.Recipients.Add(mails);
                 }
                 recipient.Type = (int)OlMeetingRecipientType.olRequired;
                 try
                 {
                     ((_AppointmentItem)agendaMeeting).Send();
                     return("Success");
                 }
                 catch (System.Exception ex)
                 {
                     return("Fail");
                 }
             }
         }
     }
     catch (System.Exception ex) { }
     return(string.Empty);
 }
Example #3
0
        public string SMTPEvent(eAppointment appointment)
        {
            if (appointment.Email.Count() > 0)
            {
                MailMessage msg = new MailMessage();
                foreach (var recepient in appointment.Email)
                {
                    msg.To.Add(recepient);
                }
                msg.From       = new MailAddress("*****@*****.**", "Canarys Automation Pvt ltd.");
                msg.Subject    = appointment.Subject;
                msg.Body       = appointment.Body;
                msg.IsBodyHtml = true;


                StringBuilder str = new StringBuilder();

                str.AppendLine("BEGIN:VCALENDAR");
                str.AppendLine("PRODID:-//Schedule a Meeting");
                str.AppendLine("VERSION:2.0");
                str.AppendLine("METHOD:REQUEST");
                str.AppendLine("BEGIN:VEVENT");
                str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", appointment.Start));
                str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
                str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", appointment.End));
                str.AppendLine("LOCATION: " + appointment.Location);
                str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
                str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body));
                str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));
                str.AppendLine(string.Format("SUMMARY:{0}", "Visit Created"));
                str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", "*****@*****.**"));
                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");


                byte[]       byteArray = Encoding.ASCII.GetBytes(str.ToString());
                MemoryStream stream    = new MemoryStream(byteArray);


                System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(stream, "Visit Invite.ics");
                msg.Attachments.Add(attach);


                System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
                contype.Parameters.Add("method", "REQUEST");
                //  contype.Parameters.Add("name", "Meeting.ics");
                AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
                AlternateView HTMLV = AlternateView.CreateAlternateViewFromString(msg.Body, new System.Net.Mime.ContentType("text/html"));
                msg.AlternateViews.Add(avCal);
                msg.AlternateViews.Add(HTMLV);


                try
                {
                    //Now sending a mail with attachment ICS file.
                    System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
                    smtpclient.Host        = "smtp.gmail.com"; //-------this has to given the Mailserver IP
                    smtpclient.Port        = 587;
                    smtpclient.EnableSsl   = true;
                    smtpclient.Credentials = new System.Net.NetworkCredential("*****@*****.**", "Canarys123");
                    smtpclient.Send(msg);
                    return("Success");
                }
                catch (System.Exception)
                {
                    return("Fail");
                }
            }
            return("No Recepient Found");
        }
        public IActionResult Edit(int id, [Bind] VisitViewModel editedVisitor, string SecondaryValues, int PrimaryValues)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (SecondaryValues != null)
                    {
                        string[] array = SecondaryValues.Split(",");
                        editedVisitor.SecondaryParticipant = new List <int>();
                        for (int i = 0; array.Length > i; i++)
                        {
                            editedVisitor.SecondaryParticipant.Add(Convert.ToInt32(array[i]));
                        }
                    }

                    //if (PrimaryValues != 0)
                    //{
                    //    editedVisitor.PrimaryParticipant = PrimaryValues;
                    //}

                    var previousresult = repository.EditGet(id);
                    _context = new VisitorManagementContext();



                    string filepath = Path.Combine(Directory.GetCurrentDirectory(), "EmailTemplates", "EmailTemplate.html");
                    string template = System.IO.File.ReadAllText(filepath);

                    string Bodymsg = string.Empty;

                    template = template.Replace("#USERNAME#", " All");
                    template = template.Replace("#LINKTOREPLACE#", "");
                    template = template.Replace("#LINKADDRESS#", "");
                    template = template.Replace("#LINKTEXT#", "");
                    template = template.Replace("#CREATEDUSERNAME#", HttpContext.Session.GetString("UserName"));
                    string body = template;
                    body = template;
                    if (previousresult.visit.VisitorName != editedVisitor.VisitorName)
                    {
                        Bodymsg += "Visitor Name has been Changed from " + previousresult.visit.VisitorName + " to " + editedVisitor.VisitorName + " .<br/>";
                        Console.WriteLine();
                    }
                    if (previousresult.visit.EmailAddress != editedVisitor.EmailAddress)
                    {
                        Bodymsg += "Visitor EmailAddress has been Changed from " + previousresult.visit.EmailAddress + " to " + editedVisitor.EmailAddress + " .<br/>";
                        Console.WriteLine();
                    }
                    if (previousresult.visit.Company != editedVisitor.Company)
                    {
                        Bodymsg += "Visitor Company has been Changed from " + previousresult.visit.Company + " to " + editedVisitor.Company + " .<br/>";
                        Console.WriteLine();
                    }
                    if (previousresult.visit.Designation != editedVisitor.Designation)
                    {
                        Bodymsg += "Visitor Designation has been Changed from " + previousresult.visit.Designation + " to " + editedVisitor.Designation + " .<br/>";
                        Console.WriteLine();
                    }
                    if (previousresult.visit.Phone != editedVisitor.Phone)
                    {
                        Bodymsg += "Visitor Phone number has been Changed from " + previousresult.visit.Phone + " to " + editedVisitor.Phone + " .<br/>";
                        Console.WriteLine();
                    }



                    if (previousresult.visitDetails.StartTime.ToShortDateString() != editedVisitor.VisitingDate.ToShortDateString())
                    {
                        Bodymsg += "Visit Date has been Changed from " + previousresult.visitDetails.StartTime.ToShortDateString() + " to " + editedVisitor.VisitingDate.ToShortDateString() + " .<br/>";
                        Console.WriteLine();
                    }
                    if (previousresult.visitDetails.StartTime.ToShortTimeString() != editedVisitor.StartTime.ToShortTimeString())
                    {
                        Bodymsg += "Visit Start Time has been Changed from " + previousresult.visitDetails.StartTime.ToShortTimeString() + " to " + editedVisitor.StartTime.ToShortTimeString() + " .<br/>";
                        Console.WriteLine();
                    }
                    if (previousresult.visitDetails.EndTime.ToShortTimeString() != editedVisitor.EndTime.ToShortTimeString())
                    {
                        Bodymsg += "Visit End Time has been Changed from " + previousresult.visitDetails.EndTime.ToShortTimeString() + " to " + editedVisitor.EndTime.ToShortTimeString() + " .<br/>";
                        Console.WriteLine();
                    }


                    for (int i = 0; editedVisitor.VisitArrangement.Count > i; i++)
                    {
                        if (previousresult.VisitArrangement.Where(x => x.ArrangementId == editedVisitor.VisitArrangement[i].ArrangementId).Count() > 0)
                        {
                            var oldvalue = previousresult.VisitArrangement.Where(x => x.ArrangementId == editedVisitor.VisitArrangement[i].ArrangementId).FirstOrDefault();
                            var newvalue = editedVisitor.VisitArrangement[i];
                            if (oldvalue.Description != newvalue.Description)
                            {
                                Bodymsg += oldvalue.Name + " Arrangements has been Changed from " + oldvalue.Description + " to " + newvalue.Description + " .<br/>";
                            }

                            if (oldvalue.DelegateContactID != newvalue.DelegateContactID)
                            {
                                var delegatecontact    = _context.User.Where(r => r.UserID == newvalue.DelegateContactID).FirstOrDefault().FirstName;
                                var olddelegatecontact = _context.User.Where(r => r.UserID == oldvalue.DelegateContactID).FirstOrDefault().FirstName;



                                Bodymsg += " Delegate Contact has been changed form " + olddelegatecontact + " to " + delegatecontact + " .<br/>";
                            }
                        }
                    }


                    body = body.Replace("#BODY#", Bodymsg);
                    repository.UpdateVisitor(editedVisitor);



                    TimeSpan     ts          = editedVisitor.EndTime - editedVisitor.StartTime;
                    eAppointment appointment = new eAppointment();
                    appointment.Subject  = "Visit Changes Notification";
                    appointment.Body     = body;
                    appointment.Location = "Canarys Automation";
                    appointment.Duration = Convert.ToInt32(ts.TotalMinutes);
                    DateTime dt       = editedVisitor.VisitingDate;
                    TimeSpan timeSpan = new TimeSpan(editedVisitor.StartTime.Hour, editedVisitor.StartTime.Minute, editedVisitor.StartTime.Second);
                    editedVisitor.VisitingDate = editedVisitor.VisitingDate.Add(timeSpan);
                    appointment.Start          = Convert.ToDateTime(editedVisitor.VisitingDate);



                    _context = new VisitorManagementContext();
                    List <string> EmailIDs = new List <string>();
                    if (editedVisitor.PrimaryParticipant != 0)
                    {
                        string mailid = _context.User.Where(r => r.UserID == editedVisitor.PrimaryParticipant).FirstOrDefault().EmailAddress;
                        EmailIDs.Add(mailid);
                    }
                    if (editedVisitor.SecondaryParticipant != null && editedVisitor.SecondaryParticipant.Count > 0)
                    {
                        for (int i = 0; editedVisitor.SecondaryParticipant.Count > i; i++)
                        {
                            string EmailAddres = _context.User.Where(r => r.UserID == editedVisitor.SecondaryParticipant[i]).FirstOrDefault().EmailAddress;
                            EmailIDs.Add(EmailAddres);
                        }
                    }
                    if (editedVisitor.VisitArrangement != null && editedVisitor.VisitArrangement.Where(r => r.IsSelected == true).Count() > 0)
                    {
                        for (int i = 0; editedVisitor.VisitArrangement.Where(r => r.IsSelected == true).Count() > i; i++)
                        {
                            string mailid = _context.User.Where(r => r.UserID == editedVisitor.VisitArrangement.Where(l => l.IsSelected == true).ElementAt(i).DelegateContactID).FirstOrDefault().EmailAddress;
                            EmailIDs.Add(mailid);
                        }
                    }
                    EmailIDs.Add(editedVisitor.EmailAddress);



                    appointment.Body          = body;
                    appointment.Email         = EmailIDs.ToArray();
                    appointment.MeetingStatus = OlMeetingStatus.olMeeting;
                    outLookEvents.SMTPEvent(appointment);

                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("", "Unable to update changes. " +
                                             "Try again, and if the problem persists, " +
                                             "see your system administrator.");
                }
            }
            catch (DbUpdateException)
            {
            }
            return(View(editedVisitor));
        }
        public IActionResult Create(VisitViewModel viewModel, string SecondaryValues, int PrimaryValues)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    viewModel.CreatedBy = int.Parse(HttpContext.Session.GetString("UserID"));
                    if (!string.IsNullOrEmpty(SecondaryValues))
                    {
                        string[] array = SecondaryValues.Split(",");
                        viewModel.SecondaryParticipant = new List <int>();
                        for (int i = 0; array.Length > i; i++)
                        {
                            viewModel.SecondaryParticipant.Add(Convert.ToInt32(array[i]));
                        }
                    }

                    if (PrimaryValues != 0)
                    {
                        viewModel.PrimaryParticipant = PrimaryValues;
                    }
                    repository.AddVisitor(viewModel);

                    string filepath = Path.Combine(Directory.GetCurrentDirectory(), "EmailTemplates", "EmailTemplate.html");
                    string template = System.IO.File.ReadAllText(filepath);

                    template = template.Replace("#USERNAME#", " All");
                    template = template.Replace("#LINKTOREPLACE#", "");
                    template = template.Replace("#LINKADDRESS#", "");
                    template = template.Replace("#LINKTEXT#", "");
                    template = template.Replace("#CREATEDUSERNAME#", HttpContext.Session.GetString("UserName"));
                    string body = template;
                    body = body.Replace("#BODY#", "A Visit has been created on " + viewModel.VisitingDate.ToShortDateString() + " from " + viewModel.StartTime.ToShortTimeString() + " to " + viewModel.EndTime.ToShortTimeString() + ". Please be available.");

                    TimeSpan     ts          = viewModel.EndTime - viewModel.StartTime;
                    eAppointment appointment = new eAppointment();
                    appointment.Subject  = "Visit Create Notification";
                    appointment.Body     = body;
                    appointment.Location = "Canarys Automation";
                    appointment.Duration = Convert.ToInt32(ts.TotalMinutes);
                    DateTime dt       = viewModel.VisitingDate;
                    TimeSpan timeSpan = new TimeSpan(viewModel.StartTime.Hour, viewModel.StartTime.Minute, viewModel.StartTime.Second);
                    viewModel.VisitingDate = viewModel.VisitingDate.Add(timeSpan);
                    appointment.Start      = Convert.ToDateTime(viewModel.VisitingDate);

                    _context = new VisitorManagementContext();
                    List <string> EmailIDs = new List <string>();
                    if (viewModel.PrimaryParticipant != 0)
                    {
                        string mailid = _context.User.Where(r => r.UserID == viewModel.PrimaryParticipant).FirstOrDefault().EmailAddress;
                        EmailIDs.Add(mailid);
                    }
                    if (viewModel.SecondaryParticipant != null && viewModel.SecondaryParticipant.Count > 0)
                    {
                        for (int i = 0; viewModel.SecondaryParticipant.Count > i; i++)
                        {
                            string EmailAddres = _context.User.Where(r => r.UserID == viewModel.SecondaryParticipant[i]).FirstOrDefault().EmailAddress;
                            EmailIDs.Add(EmailAddres);
                        }
                    }
                    EmailIDs.Add(viewModel.EmailAddress);
                    appointment.Body  = body;
                    appointment.Email = EmailIDs.ToArray();
                    outLookEvents.SMTPEvent(appointment);

                    EmailIDs = new List <string>();
                    if (viewModel.VisitArrangement != null && viewModel.VisitArrangement.Where(r => r.IsSelected == true).Count() > 0)
                    {
                        StringBuilder builder = new StringBuilder();
                        for (int i = 0; viewModel.VisitArrangement.Where(r => r.IsSelected == true).Count() > i; i++)
                        {
                            var    arrangment      = viewModel.VisitArrangement.Where(r => r.IsSelected == true).ElementAt(i);
                            string arrangmentname  = _context.Arrangement.Where(r => r.ArrangementId == arrangment.ArrangementId).FirstOrDefault().Name;
                            var    delegatecontact = _context.User.Where(r => r.UserID == viewModel.VisitArrangement.Where(l => l.IsSelected == true).ElementAt(i).DelegateContactID).FirstOrDefault();
                            EmailIDs.Add(delegatecontact.EmailAddress);
                            builder.AppendLine(arrangmentname + " Arrangements " + " for these description " + arrangment.Description + " by " + delegatecontact.FirstName);
                        }
                        body = template;
                        body = body.Replace("#BODY#", "A Visit has been created on " + viewModel.VisitingDate.ToShortDateString() + " from " + viewModel.StartTime.ToShortTimeString() + " to " + viewModel.EndTime.ToShortTimeString() + ". Please be available. <br /> Please Arrange the below items <br />  " + builder.ToString());

                        appointment.Body          = body;
                        appointment.Email         = EmailIDs.ToArray();
                        appointment.MeetingStatus = OlMeetingStatus.olMeeting;
                        outLookEvents.SMTPEvent(appointment);
                    }
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("", "Unable to save changes. " +
                                             "Try again, and if the problem persists " +
                                             "see your system administrator.");
                }
            }
            catch (DbUpdateException ex)
            {
                throw ex;
            }
            return(View(viewModel));
        }