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)
{
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");
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" };
System.IO.File.WriteAllLines(Server.MapPath("Sample.ics"), contents);
SendMail();
}
2)To Attach the file to an Email message :
This is a widely used emailing technique.
using System.Net.Mail;
public void SendMail()
{
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.";
Attachment mailAttachment = new Attachment(Server.MapPath("Sample.ics"));
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.
If you open the file,the .ics file created by us would be found as an attachment.
On opening the file,the appointment can be viewed as shown below and can also be saved into outlook.
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!!