Home » C# » [C#] calendar invite/appointment as email attachment (.net)

[C#] calendar invite/appointment as email attachment (.net)


Create Calendar Invite Content – C# code


1) Just “copy & paste” the below code and modify – Start & End meeting datetime, meeting link/ location, body content, subject text, sender & attendee email ids.



StringBuilder strInvite = new StringBuilder();

strInvite.AppendLine("BEGIN:VCALENDAR");
strInvite.AppendLine("PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN");
strInvite.AppendLine("VERSION:2.0");
strInvite.AppendLine(string.Format("METHOD:{0}", (isCancel ? "CANCEL" : "REQUEST")));
strInvite.AppendLine("METHOD:REQUEST");
strInvite.AppendLine("BEGIN:VEVENT");

strInvite.AppendLine(string.Format("CREATED:{0:yyyyMMddTHHmmssZ}", DateTime.Now.ToUniversalTime()));

// Meeting Start DateTime
strInvite.AppendLine(string.Format("DTSTART:{0}", DateTime.Now.ToUniversalTime().ToString("yyyyMMddTHHmmssZ")));

strInvite.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmss}", DateTime.Now.ToUniversalTime()));

// Meeting End DateTime
strInvite.AppendLine(string.Format("DTEND:{0:}", DateTime.Now.AddHours(1).ToUniversalTime().ToString("yyyyMMddTHHmmssZ")));

strInvite.AppendLine(string.Format("LAST-MODIFIED:{0:yyyyMMddTHHmmssZ}", DateTime.Now.ToUniversalTime()));

// web meeting link
strInvite.AppendLine(string.Format("LOCATION: {0}", eventUrlLink));
//or
//strInvite.AppendLine(string.Format("LOCATION: {0}", eventLocationLink));

strInvite.AppendLine("PRIORITY: 5");
strInvite.AppendLine("SEQUENCE: 0");

strInvite.AppendLine(string.Format("UID:{0}", (eventID.HasValue ? "EventId" + eventID : Guid.NewGuid().Tostring())));

// meeting realted message body content
strInvite.AppendLine(string.Format("DESCRIPTION:{0}", strMessageBody.Replace("\n", "
"))); strInvite.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", strMessageBody.Replace("\n", "
"))); // meeging subject text strInvite.AppendLine(string.Format("SUMMARY:{0}", subjectText)); strInvite.AppendLine("STATUS:CONFIRMED"); // sender email id strInvite.AppendLine(string.Format("ORGANIZER;CN={0}:MAILTO:{1}", strMailFrom, strMailFrom)); // attendee email ids for meeting strInvite.AppendLine(string.Format("ATTENDEE;CN={0};RSVP=TRUE:mailto:{1}", string.Join(",", strAttendeeMailTo), string.Join(",", strAttendeeMailTo))); strInvite.AppendLine("BEGIN:VALARM"); strInvite.AppendLine("TRIGGER:-PT15M"); strInvite.AppendLine("ACTION:DISPLAY"); strInvite.AppendLine("DESCRIPTION:Reminder"); strInvite.AppendLine("END:VALARM"); strInvite.AppendLine("END:VEVENT"); strInvite.AppendLine("END:VCALENDAR");


2) Attach created Calendar invite to mail as (.ics) attachment



// create instance of MailMessage object
MailMessage mailMessage = new MailMessage
{
	// fill normal mail body content
	From = new MailAddress(strMailFrom, strSenderName),
	Subject = strSubject,
	Body = strBodyContent,
	IsBodyHtml = true
};

// attach meeting request calendar invite .ics file
byte[] byteArray = Encoding.ASCII.GetBytes(strInvite);
MemoryStream stream = new MemoryStream(byteArray);
Attachment attach = new Attachment(stream, "event-meeting.ics");
mailMessage.Attachments.Add(attach);

// send mail
SmtpClient smtpClient = new SmtpClient
{
	// your credentials
};

smtpClient.Send(mailMessage);



output:
calendar invite appointment .ics file C#

Leave a Reply

Your email address will not be published. Required fields are marked *