So you know how to send an email using C# but now if you would like to attach an invitation to a meeting. We need to follow the below steps to accomplish this and it will work for the emailing apps like Outlook and Gmail.
In this code snippet I will be using an ICS(Internet Calendar Scheduling) or an iCal format for the invitation.
What is ICS
An ICS (Internet Calendar Scheduling) file is a calendar file with an universal calendar format and it is used by several email providers and calendar programs, including Microsoft Outlook, Google Calendar, Notes and Apple Calendar. It enables users to publish and share calendar information on the web and over email. ICS files are often used for sending meeting requests to other users, who can import the events into their own calendars.
To Send calendar invitation we need to use the System.Net.Mail namespace in .Net. And the classes required to send calendar invite are AlternateView & LinkedResource. For more information on these classes you can refer to Microsoft documentation
ICS Sample Format Structure
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//A//B//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
ORGANIZER;CN="Organizer":mailto:#FROM#
ATTENDEE;CN="Attendee";CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=
NEEDS-ACTION;RSVP=TRUE:mailto:#TO#
DTSTART:#DTSTART#
DTEND:#DTEND#
LOCATION:#LOCATION#
SUMMARY: Invitation for Meeting
TRANSP:OPAQUE
SEQUENCE:0
UID:#UID#
DTSTAMP:#CREATED-AT#
CREATED:#CREATED-AT#
LAST-MODIFIED:#CREATED-AT#
DESCRIPTION: #DESCRIPTION#
X-ALT-DESC;FMTTYPE=text/html: #X-ALT-DESC#
PRIORITY:5
CLASS:PUBLIC
END:VEVENT
END:VCALENDARAlternateView: AlternateView represents the format to view the email message. We can use this class to specify the email message in different formats. For example, If we are sending email in HTML format, we need to provide the plain text message also incase email readers unable to display HTML format.
LinkedResource:
It represents an embedded resource in email attachment(Ex: Image).
Below is the sample code to send meeting invitation with gmail SMTP server and you can download full code from my Github repository.
Here is the sample email invitation screenshot after sending the calendar invite from C# code.
str.AppendLine("BEGIN:VCALENDAR"); str.AppendLine("PRODID:-//A//Outlook MIMEDIR//EN"); str.AppendLine("VERSION:2.0"); str.AppendLine("METHOD:REQUEST"); str.AppendLine("BEGIN:VEVENT"); str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime1)); str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow)); str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime1)); str.AppendLine(string.Format("LOCATION: {0}", "Location")); // UID should be unique. 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("STATUS:CONFIRMED"); str.AppendLine("BEGIN:VALARM"); str.AppendLine("TRIGGER:-PT15M"); str.AppendLine("ACTION:Accept"); str.AppendLine("DESCRIPTION:Reminder"); str.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY"); str.AppendLine("END:VALARM"); str.AppendLine("END:VEVENT"); 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("END:VCALENDAR"); System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar"); ct.Parameters.Add("method", "REQUEST"); ct.Parameters.Add("name", "meeting.ics"); AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), ct); msg.AlternateViews.Add(avCal); sc.ServicePoint.MaxIdleTime = 2; sc.Send(msg);

Comments
Post a Comment