Send a Meeting Request with System.Net.Mail

The calendar appointment email is just normal email attached with ics file contains appointment details.
This code will show you how to create simple ics file and how to attach it with the email.

SmtpClient sc = new SmtpClient();
MailMessage msg = new MailMessage();
msg.From = new MailAddress("ahmed_dagga_84@hotmail.com", "Ahmed Abu Dagga");
msg.To.Add(new MailAddress("youremail@host.com", "Your Name"));
msg.Subject = "Send Calendar Appointment Email";
msg.Body = "Here is the Body Content";

StringBuilder str = new StringBuilder();
str.AppendLine("BEGIN:VCALENDAR");
str.AppendLine("PRODID:-//Ahmed Abu Dagga Blog");
str.AppendLine("VERSION:2.0");
str.AppendLine("METHOD:REQUEST");
str.AppendLine("BEGIN:VEVENT");
str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime));
str.AppendLine("LOCATION: Dubai");
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 ct = new System.Net.Mime.ContentType("text/calendar");
ct.Parameters.Add("method", "REQUEST");
AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), ct);
msg.AlternateViews.Add(avCal);

sc.Send(msg);


The calendar text lines limit is 76 character if the body or any line more than that you have to split it to more lines the first line must be 76 character and the others is 75 with tab space in the begin of the line "\t" in C# and vbTab in VB.

I hope the code is clear and If you have any question just add a comment in this blog.

Comments (46)

  1. Ahmed Abudaqqa says:

    By this way the body content will be plain text and will convert the html to palin text so if you need to add html body you have to add it with AlternateView and set the content type as text/html

    AlternateView body = AlternateView.CreateAlternateViewFromString(htmlcontent, new ContentType("text/html"));
    msg.AlternateViews.Add(body);

  2. chris.leppong says:

    Hi, Ahmed.
    This articles helps me alot.
    Currently im working on a task to send appointment via email but with no reminder.
    I removed the begein:valarm section, but it still set the reminder to 15 minutes both in outlook 2003 and 2007.

    Any suggestions? i also tried to set my outlook calender default reminder off but still the reminder set to 15minutes.
    Any idea what should i do?
    Thanks in advance

    Regards,
    Chris

  3. Ahmed Abudaqqa says:

    Hi Chris,
    If you remove the code from begin:valarm to end:valarm and disable the default reminder in the outlook, you will not get a reminder I tried it and it works fine with outlook 2007

  4. STR says:

    I copied your code exactly from above, but everytime I run it, I only get a text email and no meeting request. Am I missing something? I did have to add a host to the code as well as credentials. but that shouldnt matter right?

  5. STR says:

    Nevermind, it was an issue with the time.

  6. Roger says:

    Abu,

    This is simple, clean and great!

    I used to program about 30 years ago. So I am technical, but don't know Visual Basic, etc. However, once I have some code, I can improvise for my purpose. How do I 'run' this code? What environment do I use?

    Thanks in advance for the help.

  7. Ahmed Abudaqqa says:

    Roger,

    This is a C# code which is needs to install .NET framework and the best tool for developing .NET programs is the Visual Studio .NET and there is a free copy called Express edition.

  8. Roger says:

    Ahmed.

    THANK YOU!!
    Wishing you a lot of success with your blog.

  9. foreclass says:

    Great post, thank you very much
    However, can you tell how to create an All day event.

    I have tried:

    DTSTART;VALUE=DATE:{0:yyyyMMdd}
    DTEND;VALUE=DATE:{0:yyyyMMdd}

    But it does not work. Ex: if I send an all day event from 18.Aug - 21.Aug. It become:
    18.Aug 12:00AM - 21.Aug 12:00AM without checkbox "All day event". It means that the meeting just from 18.Aug - 20.Aug

  10. rudolflamprecht says:

    Hi, I have also copied the code but received an error saying my SMTP Host is not specified.
    Does this need to be set somewhere in the code?
    Any help please?
    Thanks

  11. Ahmed Abudaqqa says:

    foreclass,

    try adding
    TRANSP:TRANSPARENT

    rudolflamprecht,

    you can setup the SMTP host in the web config otherwise you setup it in the code when you create an instance of SmtpClient class

    SmtpClient sc = new SmtpClient("Host Name or IP");

  12. Jeyakumar says:

    HI This is fine for only sending Meeting Request . Suppose i need attach Document with this Meeting Request ? but is not getting Meeting Request ,It got like ordinary Email Mesage ,Not for Appointment? how we can solve this Problem ?

    Simply Meeting Request with File attachment ?

  13. Prashant says:

    Hi,
    this is a great code
    but i am using html and in that css and images are not refecting can u help me

  14. rudolflamprecht says:

    Thanks, that worked for me.
    Another thing, can this code be used for sending meeting request to any email software? I'm using Outlook 2007, and it
    works fine. Just wondering about any other programmes like Outlook Express or Mac Mail for instance?

  15. Denis Philippe says:

    Great piece of code but we use Outlook 2007 and the user who triggered the request gets the following in their outlook so can't save the item to their calendar. "As the meeting organizer, you do not need to respond to the meeting"

    Do you know of a way to change the request so they can add it to their Outlook calendar also?

  16. Yogi says:

    hi,
    Great code..
    thank you very much it is very helpfull..
    i have one query here if i passing the startTime and endtime when i am accepting the request it is not saving the properly to that time period in calender. why is it so?

    Thanks in advance.

  17. yogi says:

    hi.
    Ahmed Abu Dagga

    i got why it is not inserting in calendar at appropriate time.i resolve the issue.
    i have one more query will you please tell me that how to remove the appointment from calendar.

  18. yogi says:

    hi Everyone.

    would you please tell me how to remove the appointment from outlook

    Thanks in advance.
    Yogi.

  19. yogi says:

    i am getting following error while sending the mail to another host.so pleasehelp to resolve the issue.

    Mailbox unavailable. The server response was: 5.7.1 <yogesh@aaryanstructcon.com>... Relaying denied. IP name lookup failed

    Thanks in advance.

  20. codemasterflex says:

    yogi, that is an smtp issue. The same thing happend to me, but i accidentaly typed the wrong address. Are you sending through an exchange server?

  21. codemasterflex says:

    yogi, That error you are getting is from your smtp server. Are you sending through an exchange server or a hosted smtp?

  22. Gaurav Agarwal says:

    Hi Ahmed, Thanks for providing this wonderful piece of code.
    I am able to update the meeting also be storing the UID, instead of creating a new one every time. I have just one more small question that how to remove/cancel a meting from the calendar which was sent through the same code only.

    Appreciate for your help and time
    Gaurav Agarwal

  23. Allan says:

    This all seems to work fine but I am getting the "AS the meeting organiser you do not need to respnd to this meeting request". Outlook 2003 is fine. Outlook 2007 seems to get this message when using Exchange 2007.

  24. Madhu says:

    Hi, i am very happy that i found ur blog, Can u please explain me the code below one:
    StringBuilder str = new StringBuilder();
    str.AppendLine("BEGIN:VCALENDAR");
    str.AppendLine("PRODID:-//Ahmed Abu Dagga Blog");
    str.AppendLine("VERSION:2.0");
    str.AppendLine("METHOD:REQUEST");
    str.AppendLine("BEGIN:VEVENT");
    str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime));
    str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
    str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime));
    str.AppendLine("LOCATION: Dubai");
    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 ct = new System.Net.Mime.ContentType("text/calendar");
    ct.Parameters.Add("method", "REQUEST");
    AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), ct);
    msg.AlternateViews.Add(avCal);

    . Here how do we add our own time, date, location etc

    Thanks in Advance,
    Madhu

  25. madhusudhan says:

    The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. 4sm2173722ywg.54

    <system.net>
    <mailSettings>
    <smtp from="xxxx.com">
    <network host="smtp.gmail.com" userName="xxxxx" password="xxxx port="587" defaultCredentials="true" />
    </smtp>
    </mailSettings>
    </system.net>
    in web.config also

  26. Sinan says:

    I was looking for a way to send ics data rather than sending it as attachment. Your post showed me the way, thanks!

  27. Shiv says:

    How to send Meeting Request with File attachment ?

  28. polgas says:

    heaven sent! cheers for ahmed!

  29. nesfrank@yahoo.com says:

    does nayone knows how to do two things:

    1. cancel a meeting that you already sent
    2. make sure the time is eastern time for start and end of meeting. any idea?
    please email e at nesfrank@yahoo.com

  30. Pooja says:

    Hi Ahmed,

    I used you code to send out meeting request.iWorks perfectly like it should but i am facing a issue i.e. when i open my webmail it displays as a simple plain text mail without the accept /Reject option...wht shld be done..

  31. Jeff says:

    Thank you so much! This code worked perfectly!

  32. Thrilendra says:

    Hi Ahmed Abu Dagga and Yogi,

                     I am also getting the same problem like appointment is sent successful but Appointments are not getting saved in Calendar. One more question how to update and cancel the same appointment.

  33. FrummaCattipt says:

    Hey, good posting!

  34. Des says:

    Hi Ahmed,

    Is there a way to Cancel the Appointment using System.Net.Mail
    I really need it for my project.

    Thanks Ahmed.
    Des

  35. Erexempix says:

    This is my first time visit at here and i am truly impressed to read all at one place.

  36. Erexempix says:

    I like to work on PHP rather than .NET, even though .NET gives the feature of drag and drop elements, except I love PHP much.

  37. Kris says:

    Hi Ahmed,

    Firstly thanks for the code. It really helped me.

    However, when I sent a meeting for say 2:00 PM, the meeting request in my mailbox shows up as 7:30 PM. Do you know why and how I can make it use the correct timezone?

  38. Ahmed Abudaqqa says:

    Hi Kris

    What I know is you can only use GMT time zone and adjust that by calculating the difference during string creation

    DateTime utcTime = DateTime.SpecifyKind(myTime, DateTimeKind.Utc)

  39. Kris says:

    Thanks Ahmed.
    this is what I'm doing right now
    str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", MeetingScheduleDate.ToUniversalTime()));

    First converting my meeting time Universal time and assigning.

  40. Tor I. says:

    I have found it more pleasant to use the DDay.iCal library instead of hand-coding the iCalendar/vCalendar data...

  41. Arif Imran says:

    Thanx, Ahmed your blog was really helpful, saved me and my team great amount of time and effort .

  42. vijay says:

    hi Ahmed,

    I copied above code but i will get ics file as attchment i have to download first and import into my calender. but i want to directly add to calender as remainder. i am missing anything plese help..




    Thanx
    vijay

  43. Shyamjith says:

    Hi,,

    Thanks for your post. helped me a lot.

    I have a doubt

    How can we change the Appointment Timing according to DST(Daylight Saving Time)

    Thanks

    Shyam

  44. Shyamjith says:

    Hi Ahmed,,

    Thanks for this blog. helped me very much

    and I have few doubts int this

    1.Is there any way to cancel an appointment which is created  using System.Net.Mail?
    2.The DST is Making Some problems When create an Appointment in application and Sending to client The time is configured in their outlook is one hour later than my actual appointment time

    Is there any way to solve these issues?

    Plz replay asap

    Thanks & regards

    Shyam

  45. Nehal says:

    Hi Ahmed,

    Thanks a lot.

  46. ErSehmi says:

    Hi Ahmed,

    Thanks for the great code. Is there any way to cancel and update the already sent appointment invitation using System.Net.Mail.

    Regards,
    ErSehmi

Leave a Comment