Hi,
i want to send the email to each of the recipient with ICS file.
I have a list of email as recipient (abc@gmail.com , def@gmail.com) and sender is default@gmail.com
Since im using ICS, only one recipient can see my email content but i want all the recipient can read the content too. I tried to do one to want (sender to each recipient) but it didn't work.
        [HttpPost]
        public ActionResult Index(int[] chkST)
        {
            try
            {
                foreach (var item in chkST)
                {
                    var audit = db.EnMS.Where(c => c.ID == item).FirstOrDefault();
                    var join = audit.AuditeeEmail + "," + audit.AuditorEmail;
                    for (int i = 0; i < join.Split(',').Length; i++)
                    {
                        var email = join.Split(',')[i];
                        string EmailFrom = "default@gamil.com";
                        string subject = " Internal Audit : " + audit.Scope;
                        MailMessage message = new MailMessage();
                        message.To.Add(email);
                  
                        message.From = new MailAddress(EmailFrom, "Default Audit");
                        message.Subject = subject;
                        message.IsBodyHtml = true;
                        string date = Convert.ToDateTime(audit.AuditDate).ToString("MM/dd/yyyy");
                        string start = Convert.ToDateTime(audit.AuditStartTime).ToString("hh:mm tt");
                        string end = Convert.ToDateTime(audit.AuditEndTime).ToString("hh:mm tt");
                        string now = DateTime.Now.ToString("yyyyMMddTHHmmssZ");
                        string dtStart = date + " " + start;
                        string dtEnd = date + " " + end;
                        DateTime startM = Convert.ToDateTime(dtStart);
                        DateTime endM = Convert.ToDateTime(dtEnd);
                        string DateFormat = "yyyyMMddTHHmmssZ";
                        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("DTSTART:" + startM.ToUniversalTime().ToString(DateFormat));
                        str.AppendLine("DTSTAMP:" + now);
                        str.AppendLine("DTEND:" + endM.ToUniversalTime().ToString(DateFormat));
                        str.AppendLine("LOCATION: TBD ");
                        str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
                        str.AppendLine(string.Format("DESCRIPTION: Hi,\n Appointment for Internal Audit on the below for you to audit. \n\n  Date : " + date +
                            "\n Time : " + start + " - " + end + "\n Management System : " + audit.System + "\n Department : " + audit.Department + "\n Scope : " + audit.Scope +
                            "\n Auditor : " + audit.AuditorName + "\n Auditee : " + audit.AuditeeName));
                        str.AppendLine(string.Format("SUMMARY:{0}", message.Subject));
                        str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", message.From.Address));
                        
                        for (int j = 0; j < message.To.Count; i++)
                        {
                            str.AppendLine(string.Format("ATTENDEE;ROLE=REQ-PARTICIPANT;CN=\"{0}\";RSVP=TRUE:mailto:{1}", message.To[j].DisplayName, message.To[j].Address));
                        }
                        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");
                        ContentType contype = new ContentType("text/calendar");
                        contype.Parameters.Add("method", "REQUEST");
                        contype.Parameters.Add("name", "APCC Internal Audit.ics");
                        AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
                        message.AlternateViews.Add(avCal);
                        message.Headers.Add("Content-class", "urn:content-classes:calendarmessage");
                        SmtpClient smtp = new SmtpClient();
                        smtp.Send(message);
                    }
                    var updateChkbox = db.EnMS.Where(x => x.ID == item).FirstOrDefault();
                    updateChkbox.SelectEmail = true;
                    db.SaveChanges();
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        System.Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
            return RedirectToAction("Index", "Home");
        }