Hi all
I want to send sample.ics file as an mail attachment. I tried the below code but not working.
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
string schLocation = "Conference Room";
string schSubject = "Business visit discussion";
string schDescription = "Schedule description";
System.DateTime schBeginDate = Convert.ToDateTime("7/8/2011 10:00:00 AM");
System.DateTime schEndDate = Convert.ToDateTime("7/8/2011 10: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("D:\\mail\\sample1.ics", contents);
m.From = new MailAddress("frmmail@gmail.com", "displayname");
m.To.Add(new MailAddress("tomail@gmail.com", "displayname");
m.Subject = "Hi I have attached sample.ics file";
m.IsBodyHtml = true;
m.Body = "This is a Test Mail";
FileStream fs = new FileStream("D:\\mail\\sample1.ics", FileMode.Open, FileAccess.Read);
Attachment a = new Attachment(fs,"sample1.ics", MediaTypeNames.Application.Octet);
m.Attachments.Add(a);
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new System.Net.NetworkCredential("from@gmail.com","Password");
sc.EnableSsl = true;
sc.Send(m);
Console.WriteLine("Sent");
Console.ReadLine();
I can able to send mail if i comment out the line
m.Attachments.Add(a);
can anyone please tell me where i had done mistake...