How to attach a meeting schedule of outlook in asp.net via SMTP Mail
C# Code
public void SendMail(string tomailid,string resume)
{
try
{
string Body = "Your Mail Body ";
//Attach Calender
string schLocation = "Conference Room";
string schSubject = "Your Subject";
string schDescription ="Description" ;
System.DateTime schBeginDate = Convert.ToDateTime(txtDate.Text +" "+ txtTime.Text);
System.DateTime schEndDate = schBeginDate.AddMinutes(30);
//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*/
Random rnd = new Random();
string path = "~/MeetingFiles/" +rnd.Next().ToString()+".ics";
System.IO.File.WriteAllLines(Server.MapPath(path), contents);
//string name = Session["UserName"].ToString();
MailMessage mail = new MailMessage();
mail.To.Add(tomailid);
mail.From = new MailAddress("Your Mail Id");
mail.Subject = "You have 1 scheduled Meeting";
mail.CC.Add("Mail CC if any");
mail.Attachments.Add(new Attachment(Server.MapPath(resume))); // attach the File
Attachment mailAttachment = new Attachment(Server.MapPath(path));
mail.Attachments.Add(mailAttachment);
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.Host = "outlook.office365.com";
smtp.Credentials = new System.Net.NetworkCredential
("Your Id", "Your Password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
}
}
