Creating Ical Files in C#

How to create .ics file using c#?

This is how I usually create .ics files.

//some variables for demo purposes
DateTime DateStart = DateTime.Now;
DateTime DateEnd = DateStart.AddMinutes(105);
string Summary = "Small summary text";
string Location = "Event location";
string Description = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
string FileName = "CalendarItem";

//create a new stringbuilder instance
StringBuilder sb = new StringBuilder();

//start the calendar item
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("VERSION:2.0");
sb.AppendLine("PRODID:stackoverflow.com");
sb.AppendLine("CALSCALE:GREGORIAN");
sb.AppendLine("METHOD:PUBLISH");

//create a time zone if needed, TZID to be used in the event itself
sb.AppendLine("BEGIN:VTIMEZONE");
sb.AppendLine("TZID:Europe/Amsterdam");
sb.AppendLine("BEGIN:STANDARD");
sb.AppendLine("TZOFFSETTO:+0100");
sb.AppendLine("TZOFFSETFROM:+0100");
sb.AppendLine("END:STANDARD");
sb.AppendLine("END:VTIMEZONE");

//add the event
sb.AppendLine("BEGIN:VEVENT");

//with time zone specified
sb.AppendLine("DTSTART;TZID=Europe/Amsterdam:" + DateStart.ToString("yyyyMMddTHHmm00"));
sb.AppendLine("DTEND;TZID=Europe/Amsterdam:" + DateEnd.ToString("yyyyMMddTHHmm00"));
//or without
sb.AppendLine("DTSTART:" + DateStart.ToString("yyyyMMddTHHmm00"));
sb.AppendLine("DTEND:" + DateEnd.ToString("yyyyMMddTHHmm00"));

sb.AppendLine("SUMMARY:" + Summary + "");
sb.AppendLine("LOCATION:" + Location + "");
sb.AppendLine("DESCRIPTION:" + Description + "");
sb.AppendLine("PRIORITY:3");
sb.AppendLine("END:VEVENT");

//end calendar item
sb.AppendLine("END:VCALENDAR");

//create a string from the stringbuilder
string CalendarItem = sb.ToString();

//send the calendar item to the browser
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "text/calendar";
Response.AddHeader("content-length", CalendarItem.Length.ToString());
Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + ".ics\"");
Response.Write(CalendarItem);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();

Create ics file and send email with Attachment using c#

I removed all the code and enter the code after testing it on my machine. This is working fine in my case. In your case it's not working try to setup Less secure app in Gmail (I see you are using gmail to send emails)

System.Net.Mail.MailMessage msg = new MailMessage("anirudhagupta01@example.com","anirudha@testing.com");

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: " + "abcd");
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");

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

Attachment attach = new Attachment(stream,"test.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);
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("anirudhagupta0011@gmail.com", "testing");
smtpclient.Send(msg);

How to make .ics file as downloadable c#

The documentation supports using a stream. You could write the contents of the calendar to the response. Make sure you set the mime type as well.

iCalendarSerializer serializer = new iCalendarSerializer(iCal);
serializer.Serialize(Response.OutputStream, Encoding.ASCII);

http://www.ddaysoftware.com/Pages/Projects/DDay.iCal/Documentation/



Related Topics



Leave a reply



Submit