Click here to Skip to main content
Click here to Skip to main content

Send Calendar Appointment As Email Attachment

, 2 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

    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.

    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.

    <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)

    Share

    About the Author

    Swatiln
    Web Developer
    India India
    No Biography provided

    Comments and Discussions

     
    QuestionAdd ReminderMinutesBeforeStart Pinmemberriovkiheller11-Sep-13 2:06 
    QuestionHow to add formatted text to description in ics file Pinmembersob18-Jul-13 2:56 
    SuggestionUse MemoryStream PinmemberMember 460686223-Oct-12 12:44 
    GeneralMy vote of 1 PinmemberSyed Javed13-Sep-11 19:31 
    AnswerThis article explane how to show the calendar directly without open the attachment [modified] PinmemberAhmed Abu Dagga17-May-09 1:38 
    Questionnice article PinmemberRed October 717-Mar-09 23:47 
    Generalcannot open ics Pinmembercpt.oneeye8-Sep-08 21:59 
    GeneralDispose Attachment object (suggestion) Pinmembermrb210717-Jul-08 10:01 
    GeneralWell done. PinmemberYashar Sadiq4-Jul-08 1:18 
    Generalsuggestions Pinmemberkroach3-Jul-08 7:06 
    GeneralRe: suggestions PinmemberSwatiln3-Jul-08 20:44 
    GeneralGreat Pinmembermerlin9813-Jul-08 6:49 
    GeneralNice artical PinmemberGautam Sharma3-Jul-08 1:03 
    GeneralGood Start Though! Pinmembersantosh poojari3-Jul-08 0:53 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.141223.1 | Last Updated 2 Jul 2008
    Article Copyright 2008 by Swatiln
    Everything else Copyright © CodeProject, 1999-2014
    Layout: fixed | fluid