Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Send Calendar Appointment As Email Attachment

0.00/5 (No votes)
2 Jul 2008 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here