Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET

Send appointment through email in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.96/5 (32 votes)
20 Nov 2012CPOL1 min read 170.2K   5.8K   79   28
Send appointment or event through mail, so it can be synchronized with Outlook calendar
SendAppointment/event1.png

Introduction

Nowadays, event management is becoming a very popular idea especially in case of web application development. Most of the application are synchronizing data with Outlook contacts and calendar. Here is a small tool to create hourly and daily events with send email in asp.net. This concept is very simple creating an ICS file and sending as mail attachment.

Background

I was developing a web application which is related to event management and my client requirement is to send day and hour events so it will be synchronized with client’s Outlook calendar.

Send Event/Appointment

In Outlook, two types of events/appointments can be sent.

  1. Hourly event (based on specific hour of a day)
  2. Day event (based on specific days)

Hourly Event

To send hourly event, you need to send ics file for hour event. ICS file looks like that:

SendAppointment/cal_hour_event.png

I have made a simple UI to send hour event. To load hour, use a simple control from AjaxControlToolkit. This is the simple UI.

SendAppointment/hour_event.png

Code to create ICS file for day event/appointment:

C#
public string MakeHourEvent(string subject, string location, 
	DateTime date, string startTime, string endTime)
   {
    string filePath = string.Empty;
    string path = HttpContext.Current.Server.MapPath(@"\iCal\");
    filePath = path + subject + ".ics";
    writer = new StreamWriter(filePath);
    writer.WriteLine("BEGIN:VCALENDAR");
    writer.WriteLine("VERSION:2.0");
    writer.WriteLine("PRODID:-//hacksw/handcal//NONSGML v1.0//EN");
    writer.WriteLine("BEGIN:VEVENT");
    string startDateTime = GetFormatedDate(date)+"T"+GetFormattedTime(startTime);
    string endDateTime = GetFormatedDate(date) + "T" + GetFormattedTime(endTime);
    writer.WriteLine("DTSTART:" + startDateTime);
    writer.WriteLine("DTEND:" + endDateTime);
    writer.WriteLine("SUMMARY:" + subject);
    writer.WriteLine("LOCATION:" + location);
    writer.WriteLine("END:VEVENT");
    writer.WriteLine("END:VCALENDAR");
    writer.Close();
    return filePath;
   }

Daily Event

To send daily event, you need to send ICS file. This file looks like that:

SendAppointment/cal_day_event.png

I have made a simple UI using an Ajaxtoolkit CalendarExtender. Here, the user can select a different date from calendar and in case of day event creation, there is no need to time.

SendAppointment/day_event.png

Here is the code to create day event.

C#
public string MakeDayEvent
	(string subject, string location, DateTime startDate, DateTime endDate)
   {
    string filePath = string.Empty;
    string path = HttpContext.Current.Server.MapPath(@"\iCal\");
    filePath = path + subject + ".ics";
    writer = new StreamWriter(filePath);
    writer.WriteLine("BEGIN:VCALENDAR");
    writer.WriteLine("VERSION:2.0");
    writer.WriteLine("PRODID:-//hacksw/handcal//NONSGML v1.0//EN");
    writer.WriteLine("BEGIN:VEVENT");
    string startDay = "VALUE=DATE:" + GetFormatedDate(startDate);
    string endDay = "VALUE=DATE:" + GetFormatedDate(endDate);
    writer.WriteLine("DTSTART;" + startDay);
    writer.WriteLine("DTEND;" + endDay);
    writer.WriteLine("SUMMARY:" + subject);
    writer.WriteLine("LOCATION:" + location);
    writer.WriteLine("END:VEVENT");
    writer.WriteLine("END:VCALENDAR");
    writer.Close();
    return filePath;        
   }

The code to send mail with attachment is as follows:

C#
public void SendMail(string from, string to, 
	string subject, string body, Attachment attachment)
   {
    MailMessage mail = new MailMessage(from, to, subject, body);
    mail.Attachments.Add(attachment);
    SmtpClient smtp = new SmtpClient("localhost");
    smtp.Send(mail);
    mail.Dispose();
   }

The complete code of that tool is available in the code folder. Please download and try with that.

History

  • Version-1 of that small tool

License

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


Written By
Software Developer (Senior)
Canada Canada
Software engineer with broad experience in enterprise application development, product deployment automation, software test & test automation, application & system management, and manage big projects and team using proven agile technologies.

Passionate on Microsoft technologies, developed solutions using C#, .net (1.1/2.0/3.5/4), SQL Server (2005/2008). Work on Powershell, SSRS, SSIS, WPF, Ajax, WCF, JQuery.

Develop innovative application with cutting edge technologies always boosting inside.

Comments and Discussions

 
QuestionGetFormattedtime is not working correctly Pin
zXSwordXz14-Jul-16 10:21
zXSwordXz14-Jul-16 10:21 
QuestionTask in Outlook Pin
NiteshPanj10-Sep-14 3:14
NiteshPanj10-Sep-14 3:14 
QuestionVery good Pin
Vishal_0075-Sep-14 4:44
Vishal_0075-Sep-14 4:44 
QuestionFailure sending SMTP mail Pin
Sanket Ambre13-Aug-14 18:44
Sanket Ambre13-Aug-14 18:44 
QuestionFailure sending mail : smtp.Send(mail); Pin
Aulia Rahmawati16-Jun-14 22:14
Aulia Rahmawati16-Jun-14 22:14 
QuestionCould not load file or assembly Pin
mirzanaumanbaig12-May-14 22:34
mirzanaumanbaig12-May-14 22:34 
AnswerRe: Could not load file or assembly Pin
mirzanaumanbaig13-May-14 0:18
mirzanaumanbaig13-May-14 0:18 
GeneralMy vote of 5 Pin
csharpbd20-Nov-12 19:26
professionalcsharpbd20-Nov-12 19:26 
GeneralMy vote of 5 Pin
AbdulMuheet20-Nov-12 15:59
AbdulMuheet20-Nov-12 15:59 
QuestionSmtpClient smtp = new SmtpClient("localhost"); Error Pin
karthikeyanjothi18-Oct-12 4:29
karthikeyanjothi18-Oct-12 4:29 
QuestionHow to send this remainder automatically into gmail calender Pin
vijay.vangaveti112-Feb-12 18:53
vijay.vangaveti112-Feb-12 18:53 
QuestionThanks Million Pin
bellay18-Jun-11 10:15
bellay18-Jun-11 10:15 
GeneralFailed to Map path Pin
Haridass28-May-11 3:19
Haridass28-May-11 3:19 
GeneralAppointment with color background depending on condition Pin
sonashish9-Dec-09 6:00
sonashish9-Dec-09 6:00 
Answerthis is other way send the appointment without attach it Pin
Ahmed Abu Dagga20-May-09 19:05
Ahmed Abu Dagga20-May-09 19:05 
GeneralFailed to map the path '/iCal/'. Pin
abhinash.b25-Sep-08 20:24
abhinash.b25-Sep-08 20:24 
QuestionHow to embade it in mail? Pin
Danko Greiner9-Sep-08 22:50
Danko Greiner9-Sep-08 22:50 
Generalproblem with \iCal\ Pin
Hossein Sarab1-Sep-08 6:09
Hossein Sarab1-Sep-08 6:09 
GeneralRe: problem with \iCal\ Pin
Haridass27-May-11 20:04
Haridass27-May-11 20:04 
GeneralFailed to map the path '/iCal/'. Pin
Hossein Sarab1-Sep-08 6:06
Hossein Sarab1-Sep-08 6:06 
GeneralRe: Failed to map the path '/iCal/'. Pin
Member 121150074-Nov-15 18:48
Member 121150074-Nov-15 18:48 
GeneralAjax Problem Pin
danielle661011-Jun-08 23:13
danielle661011-Jun-08 23:13 
GeneralRe: Ajax Problem Pin
danielle661011-Jun-08 23:39
danielle661011-Jun-08 23:39 
GeneralRe: Ajax Problem Pin
Ashrafur Rahaman11-Jun-08 23:50
Ashrafur Rahaman11-Jun-08 23:50 
Generalthanks! Pin
Rochelle Velasquez-Macazo29-Apr-08 15:06
Rochelle Velasquez-Macazo29-Apr-08 15:06 

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.