Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI All,

I am trying to send .ics file as an attachment to user by creating ics file and email from C# code it self.I am able to send ICS file saving in to a local drive and attaching to email.

I would like to send ics file with out saving in to any drive.

help me is there any way to send ics file from the C# page itself...

Thanks
Posted
Updated 25-Apr-23 7:06am
Comments
Tomas Takac 10-Oct-14 8:24am    
Can you post some code?
Ddev5a2 10-Oct-14 9:03am    
Here is the code which I used to send direct invite from my gmail to destination.
but now i need to send invitation as an attachment in an email.

Please let me know for more details.
Code Starts:
static void Main(string[] args)
{
Console.WriteLine("Welcome");
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myemail@gmail.com", "password"),
EnableSsl = true,
Timeout =100000
};
Console.WriteLine("connection success");
// Now Contruct the ICS file using string builder
MailMessage msg = new MailMessage();
msg.From = new MailAddress("fromemail@gmail.com", "Sender's Name");
msg.To.Add(new MailAddress("tomail@gmail.com", "To Name"));
msg.Subject = "Send mail with ICS file as an Attachment";
msg.Body = "Please Attend the meeting with this schedule";
StringBuilder str = new StringBuilder();
str.AppendLine("BEGIN:VCALENDAR");
str.AppendLine("PRODID:-//Schedule a Meeting");
str.AppendLine("VERSION:2.0");
str.AppendLine("METHOD:REQUEST");
str.AppendLine("BEGIN:VEVENT");
str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+10)));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+60)));
str.AppendLine("LOCATION: CGI");
str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body));
str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));
str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject));
str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));

str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address));

str.AppendLine("BEGIN:VALARM");
str.AppendLine("TRIGGER:-PT15M");
str.AppendLine("ACTION:DISPLAY");
str.AppendLine("DESCRIPTION:Reminder");
str.AppendLine("END:VALARM");
str.AppendLine("END:VEVENT");
str.AppendLine("END:VCALENDAR");

System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
contype.Parameters.Add("method", "REQUEST");
contype.Parameters.Add("name", "Meeting.ics");
AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);

msg.AlternateViews.Add(avCal);
client.Send(msg);
Console.WriteLine("Sent");
Console.ReadLine();
}
Tomas Takac 13-Oct-14 4:12am    
msg.Attachments(new Attachment(str.ToString(), contype));
am I missing something?
Ddev5a2 13-Oct-14 7:04am    
Thank you very much..:) I am able to send the attachment with the below.

msg.Attachments.Add(new Attachment(str.ToString(), contype));

Solution send Calender Invite as an attachment(.ics file) to end user.

Code Starts:
C#
static void Main(string[] args)
{
    Console.WriteLine("Welcome");
    var client = new SmtpClient("smtp.gmail.com", 587)
    {
        Credentials = new NetworkCredential("myemail@gmail.com", "password"),
        EnableSsl = true,
        Timeout =100000
    };
    Console.WriteLine("connection success");
    // Now Contruct the ICS file using string builder
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("fromemail@gmail.com", "Sender's Name");
    msg.To.Add(new MailAddress("tomail@gmail.com", "To Name"));
    msg.Subject = "Send mail with ICS file as an Attachment";
    msg.Body = "Please Attend the meeting with this schedule";
    StringBuilder str = new StringBuilder();
    str.AppendLine("BEGIN:VCALENDAR");
    str.AppendLine("PRODID:-//Schedule a Meeting");
    str.AppendLine("VERSION:2.0");
    str.AppendLine("METHOD:REQUEST");
    str.AppendLine("BEGIN:VEVENT");
    str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+10)));
    str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
    str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+60)));
    str.AppendLine("LOCATION: CGI");
    str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
    str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body));
    str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));
    str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject));
    str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));
 
    str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address));
 
    str.AppendLine("BEGIN:VALARM");
    str.AppendLine("TRIGGER:-PT15M");
    str.AppendLine("ACTION:DISPLAY");
    str.AppendLine("DESCRIPTION:Reminder");
    str.AppendLine("END:VALARM");
    str.AppendLine("END:VEVENT");
    str.AppendLine("END:VCALENDAR");
 
    System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
    contype.Parameters.Add("method", "REQUEST"); 
    contype.Parameters.Add("name", "Meeting.ics");
    
    // EDIT: Use the correct method to create an attachment from in-memory data:
    // msg.Attachments.Add(new Attachment(str.ToString(), contype));
    msg.Attachments.Add(Attachment.CreateAttachmentFromString(str.ToString(), contype));

    client.Send(msg);
    Console.WriteLine("Sent");
    Console.ReadLine();
}
 
Share this answer
 
v2
Comments
Arun Sahoo 4-Nov-16 3:15am    
While executing this line msg.Attachments.Add(new Attachment(str.ToString(), contype));
, it throws the exception called "Illegal characters in path."

Please suggest what went wrong.
Mashudu Nemukula 2-Oct-20 4:28am    
Also getting the same error. Anyone found what is the issue?
Member 15617754 12-Oct-22 8:41am    
Getting The Same Issue
Richard Deeming 26-Apr-23 3:34am    
Rather than:
new Attachment(str.ToString(), contype)

use:
Attachment.CreateAttachmentFromString(str.ToString(), contype)
Instead of this line: msg.Attachments.Add(new Attachment(str.ToString(), contype));,
which kept giving me an error, I wrote the contents of str to a file via...

string strName = "Aname";
DateTime dt = DateTime.Now.AddHours(1);
string strDate = dt.ToString("yyyyMMddTHHmmss");
...
string strFileName = strName + strDate + ".ics";
...
WriteToFile(sb.ToString());

//msg.Attachments.Add(new Attachment(sb.ToString(), ct));
msg.Attachments.Add(new Attachment(strFileName));

------------
private static void WriteToFile(string strText)
{
using (StreamWriter sw = new StreamWriter(strFileName, true)) // true = append
{
sw.WriteLine(strText);
}
}


Then I attached the newly created file. This worked for me.
 
Share this answer
 
Comments
Richard Deeming 26-Apr-23 3:35am    
Rather than littering your disk with temporary files that you never clean up, just use Attachment.CreateAttachmentFromString(str.ToString(), contype) to create the attachment from the in-memory data.

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