Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / C#
Article

Send Calendar Appointment As Email Attachment

Rate me:
Please Sign up or sign in to vote.
4.59/5 (15 votes)
2 Jul 2008CPOL2 min read 187.9K   71   16
This article describes the procedure to create outlook calendar appointment(.ics) file and send it through email as an attachment.

Introduction

I came across a scenario where a meeting/visit schedule had to be set in an aspx page and the same schedule had to be sent across as a calendar invite in an email. The calendar file was to be an attachment in the email. This article gives a brief description of the procedure to create an outlook calendar appointment(.ics) file and send it through email as an attachment and also to check if the method worked without actually having to configure the SMTP server to send the mail.

Steps :

Let us divide this section into 3 broad parts:

  • To Create an Outlook calendar (.ics) file.
  • To Attach the file to an Email message.
  • To Check whether the above mentioned procedure worked successfully without configuring an SMTP server.

    1)To Create an Outlook calendar(.ics) file: Now,there are methods available to create the .ics file and keep it in the memory stream and make it downloadable.However,since i wanted to attach the file,I have chosen to create a file in a physical drive and write the contents into it.

    C#
    protected void Page_Load(object sender, EventArgs e)
        {
           //INITIALIZING MEETING DETAILS
    
            string schLocation = "Conference Room";
            string schSubject = "Business visit discussion";
            string schDescription = "Schedule description";
            System.DateTime schBeginDate = Convert.ToDateTime("7/3/2008 10:00:00 PM");
            System.DateTime schEndDate = Convert.ToDateTime("7/3/2008 11:00:00 PM");
    
            //PUTTING THE MEETING DETAILS INTO AN ARRAY OF STRING
                    
            String[] contents = { "BEGIN:VCALENDAR",
                                  "PRODID:-//Flo Inc.//FloSoft//EN",
                                  "BEGIN:VEVENT",
                                  "DTSTART:" + schBeginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"), 
                                  "DTEND:" + schEndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"), 
                                  "LOCATION:" + schLocation, 
    	                     "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + schDescription,
                                  "SUMMARY:" + schSubject, "PRIORITY:3", 
    	                     "END:VEVENT", "END:VCALENDAR" };
    
            /*THE METHOD 'WriteAllLines' CREATES A FILE IN THE SPECIFIED PATH WITH 
           THE SPECIFIED NAME,WRITES THE ARRAY OF CONTENTS INTO THE FILE AND CLOSES THE
            FILE.SUPPOSE THE FILE ALREADY EXISTS IN THE SPECIFIED LOCATION,THE CONTENTS 
           IN THE FILE ARE OVERWRITTEN*/
    
            System.IO.File.WriteAllLines(Server.MapPath("Sample.ics"), contents);
            
            //METHOD TO SEND EMAIL IS CALLED
            SendMail();
    
               }

    2)To Attach the file to an Email message : This is a widely used emailing technique.

    C#
    using System.Net.Mail;
    
    public void SendMail()
        {
            //CONFIGURE BASIC CONTENTS OF AN EMAIL
    
            string FromName = "TestNameFrom";
            string FromEmail = "testNameFrom@testAddress.com";
            string ToName = "TestNameTo";
            string ToEmail = "testNameTo@testAddress.com";
    
    
            System.Net.Mail.SmtpClient smtp = new SmtpClient();
            smtp.EnableSsl = false;
            System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
            mailMessage .From = new System.Net.Mail.MailAddress(FromEmail, FromName); 
            mailMessage .To.Add(new System.Net.Mail.MailAddress(ToEmail, ToName)); 
            mailMessage .Subject = "Outlook calendar as attachment";
            mailMessage .Body = "This is a test message.";
    
            //MAKE AN ATTACHMENT OUT OF THE .ICS FILE CREATED
    
            Attachment mailAttachment = new Attachment(Server.MapPath("Sample.ics"));
    
            //ADD THE ATTACHMENT TO THE EMAIL
    
            mailMessage .Attachments.Add(mailAttachment);
            smtp.Send(mailMessage);
        } 


    3)To Check whether the above mentioned procedure worked : The technique to do that is very simple.If you observe,we did not specify an SMTP server in order to send the email across.A small piece of code in the web.config file does the trick.In this example,a folder 'test' has been made in C drive, and this has been specified as the location to dump the mail in.This is helpful when SMTP server is not configured and we need to debug the email functionality.

    XML
    <system.net>
    	<mailSettings>
    		<smtp deliveryMethod="SpecifiedPickupDirectory">
    			<specifiedPickupDirectory pickupDirectoryLocation="C:\test\"/>
    		</smtp>
    	</mailSettings>
    </system.net>
  • Once you run the code,a '.eml' file is created in the directory specified above.
    EML_file.JPG
  • If you open the file,the .ics file created by us would be found as an attachment.
    Outlook_email.JPG
  • On opening the file,the appointment can be viewed as shown below and can also be saved into outlook.
    Do_you_want_to_open.JPG

    Outlook_Appointment.JPG

    Points of Interest

    This is my first article! I had a lot of fun writing it.Hope it is of help to people who are up against the same challenge. Any suggestions,best practices,a better implementation or improvements that i can make to this code are most welcome!!

  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Web Developer
    India India
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionSend Calendar Appointment As Email Attachment Pin
    Member 103787675-Nov-18 6:38
    Member 103787675-Nov-18 6:38 
    QuestionAdd ReminderMinutesBeforeStart Pin
    riovkiheller11-Sep-13 1:06
    riovkiheller11-Sep-13 1:06 
    QuestionHow to add formatted text to description in ics file Pin
    sob18-Jul-13 1:56
    sob18-Jul-13 1:56 
    SuggestionUse MemoryStream Pin
    Member 460686223-Oct-12 11:44
    Member 460686223-Oct-12 11:44 
    GeneralMy vote of 1 Pin
    Syed J Hashmi13-Sep-11 18:31
    Syed J Hashmi13-Sep-11 18:31 
    AnswerThis article explane how to show the calendar directly without open the attachment [modified] Pin
    Ahmed Abu Dagga17-May-09 0:38
    Ahmed Abu Dagga17-May-09 0:38 
    Questionnice article Pin
    Red October 717-Mar-09 22:47
    Red October 717-Mar-09 22:47 
    nice article swat.
    is there any way that without sending the file as an attachement, the user directly views the calendar event, just the same way when someone creates the event and the user accepts/rejects it.
    Generalcannot open ics Pin
    cpt.oneeye8-Sep-08 20:59
    cpt.oneeye8-Sep-08 20:59 
    QuestionHow to include an vcf attachment in the .ics Pin
    Hrishit6-Aug-08 23:11
    Hrishit6-Aug-08 23:11 
    GeneralDispose Attachment object (suggestion) Pin
    mrb210717-Jul-08 9:01
    mrb210717-Jul-08 9:01 
    GeneralWell done. Pin
    Yashar Sadiq4-Jul-08 0:18
    Yashar Sadiq4-Jul-08 0:18 
    Generalsuggestions Pin
    kroach3-Jul-08 6:06
    kroach3-Jul-08 6:06 
    GeneralRe: suggestions Pin
    Swatiln3-Jul-08 19:44
    Swatiln3-Jul-08 19:44 
    GeneralGreat Pin
    merlin9813-Jul-08 5:49
    professionalmerlin9813-Jul-08 5:49 
    GeneralNice artical Pin
    Gautam Sharma3-Jul-08 0:03
    Gautam Sharma3-Jul-08 0:03 
    GeneralGood Start Though! Pin
    santosh poojari2-Jul-08 23:53
    santosh poojari2-Jul-08 23:53 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.