Click here to Skip to main content
15,896,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
I want to send sample.ics file as an mail attachment. I tried the below code but not working.
C#
            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...
Posted
Comments
Unareshraju 1-Aug-12 2:34am    
hi kulasekaran,
i believe what ever you did right. pls check below thinks may be we will get
1).ics file will support or not?
2)with filestream we can pass the attach file file,generally we will get lot of traffic using filestream.

You need to create MailAttachment and not just Attachment object for attachments to pass on.

Have a look at this tip: Sending an Email in C# with or without attachments: generic routine.[^]
 
Share this answer
 
v2
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900