Send Outlook Invitation C# / Send outlook invitation ASP .NET / Send Google Calendar invitation

Here are the steps: Go through the attachment, includes the libraries built in for Google data, to send the Google Calendar invitation.
 
Step 1

public class eAppointmentMail
{
      public string Name { set; get; }
      public string Email { set; get; }
      public string Location { set; get; }
      public DateTime StartDate { set; get; }
      public DateTime EndDate { set; get; }
      public string Subject { set; get; }
      public string Body { set; get; }
}

 public class specified as input to my method "sendoutlookinvitation"

Step 2

include the following code in web.config under <configuration> section

<system.net>
    <
mailSettings>
      <
smtp deliveryMethod="Network" from="[email protected]">
        <network host="smtp.gmail.com" port="25" userName="[email protected]" password="myPwd" defaultCredentials="false"/>
      </
smtp>
    </
mailSettings>
  </
system.net>

Step 3

In your c# code try using the following snippet:

public static string sendOutlookInvitationViaICSFile(eAppointmentMail objApptEmail)
{
         try
         {
             Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
             MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
 
             SmtpClient sc = new SmtpClient("smtp.gmail.com");
 
             System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
 
             msg.From = new MailAddress(settings.Smtp.Network.UserName);
             msg.To.Add(new MailAddress(objApptEmail.Email, objApptEmail.Name));
             msg.Subject = objApptEmail.Subject;
             msg.Body = objApptEmail.Body; 

             StringBuilder str = new StringBuilder();
             str.AppendLine("BEGIN:VCALENDAR");
             str.AppendLine("PRODID:-//" + objApptEmail.Email);
             str.AppendLine("VERSION:2.0");
             str.AppendLine("METHOD:REQUEST");
             str.AppendLine("BEGIN:VEVENT");
 
             str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", objApptEmail.StartDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z")));
             str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", (objApptEmail.EndDate - objApptEmail.StartDate).Minutes.ToString()));
             str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", objApptEmail.EndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z")));
             //str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", objApptEmail.StartDate.ToString()));
             //str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
             //str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", objApptEmail.EndDate.ToString()));
             str.AppendLine("LOCATION:" + objApptEmail.Location);
             str.AppendLine(string.Format("DESCRIPTION:{0}", objApptEmail.Body));
             str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", objApptEmail.Body));
             str.AppendLine(string.Format("SUMMARY:{0}", objApptEmail.Subject));
             str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", objApptEmail.Email)); 

             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 ct = new System.Net.Mime.ContentType("text/calendar");
             ct.Parameters.Add("method", "REQUEST");
             AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), ct);
             msg.AlternateViews.Add(avCal);
             NetworkCredential nc = new NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password); 
             sc.Port = settings.Smtp.Network.Port;
             sc.EnableSsl = true;
             sc.Credentials = nc;
             try
             {
                 sc.Send(msg);
                 return "Success";
             }
             catch
             {
                 return "Fail";
             }
         }
         catch { }
         return string.Empty;
  }

It will simply download an ICS file, once you click on that it will open up with outlook calendar to show your appointment created.

Ebook Download
View all
Learn
View all