Skip to main content

Send Meeting Invitation with C#

    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:VCALENDAR
AlternateView

    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

Popular posts from this blog

C# Send Email via SMTP

Simple Mail Transfer Protocol (SMTP) Simple Mail Transfer Protocol (SMTP) is a TCP/IP protocol used in sending and receiving e-mail. Most of the e-mail systems that send mail over the Internet use SMTP to send messages from one server to another. The messages can then be retrieved with an e-mail client using either POP or IMAP. SMTP Class in C#:  MailMessage class is part of the namespace System.Net.Mail and it is used to create the email messages that are sent to the SMTP Server. The delivery of the message will be taken care by the SmtpClient Class. SMTP Class Properties: Host:  Server URL for SMTP EnableSsl:  True or False. Port:  Port Number of the SMTP server Credentials:  Valid login credentials for the SMTP server (the email address and password). UseDefaultCredentials:  When we set to True then that specifies to allow authentication based on the credentials of the account used to send emails. Below are the list of few SMTP Server and Port ...

.NET 8 Minimal APIs: A Step-by-Step Guide

                      .NET 8 Minimal APIs                 In the ever-evolving world of software development, .NET 8 brings a fresh approach to building APIs with its Minimal API feature. This streamlined framework allows developers to create lightweight, high-performance APIs with minimal setup and configuration. In this blog post, we’ll explore the key features of .NET 8 Minimal APIs and walk through the steps to create your first minimal API. Why Minimal APIs?      Minimal APIs in .NET 8 are designed to simplify the development process by reducing boilerplate code and focusing on the essentials. Here are some benefits: Simplicity : Minimal APIs require less code and configuration, making them easier to set up and maintain. Performance : With fewer abstractions, Minimal APIs can offer better performance. Flexibility : Ideal for microservices and small appli...