title: Create ics file and send email with Attachment using c#
body: <p>I need to send calendar appointment email attached with ics file contains appointment details, I am getting Mails but Attachment is missing help me, someone to overcome this</p>
  
  <p>Need to create simple ics file and attach it with the email.</p>
  
  <p>Please Look at below code as I tried</p>
  
  <pre><code>   public IHttpActionResult cal(calendar objApptEmail)
      {
          MailMessage msg = new MailMessage();
          //Now we have to set the value to Mail message properties
  
          //Note Please change it to correct mail-id to use this in your application
          msg.From = new MailAddress("ssss@gmail.com", "ABC");
          msg.To.Add(new MailAddress("ssss@outlook.com", "BCD"));
          //  msg.CC.Add(new MailAddress("zzzzz@xyz.com", "DEF"));// it is optional, only if required
          //   msg.Headers.Add("Content-class", "urn:content-classes:calendarmessage");
          msg.Subject = "Send mail with ICS file as an Attachment";
          msg.Body = "Please Attend the meeting with this schedule";
  
          // Now Contruct the ICS file using string builder
          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}", DateTime.Now.AddMinutes(+330)));
          str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
          str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+660)));
          str.AppendLine("LOCATION: " + objApptEmail.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}", 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("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", "REQUEST");
          //  contype.Parameters.Add("name", "Meeting.ics");
          AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
          msg.AlternateViews.Add(avCal);
  
          //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.EnableSsl = true;
          smtpclient.Credentials = new System.Net.NetworkCredential("dddd@gmail.com", "xxxxx");
          smtpclient.Send(msg);
          return Ok();       
  
  }
  </code></pre>