Send Calendar Appointment As Email Attachment
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:
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>
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!!