If you want to send a user friendly appointment request via your SMTP server instead of boring email that we usually do then we can achieve it with the help of very simple code.
Here is code:
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
namespace CalendarEvent
{
class Program
{
static void Main(string[] args)
{
Program.SendCalenderInvite();
}
public static void SendCalenderInvite()
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("xyz@gmail.com", "xyz");
msg.To.Add(new MailAddress("abc@gmail.com", "abc,"));
msg.Subject = "Appointment with xyz";
msg.Body = "Please find the time for this meeting";
// Now Contruct the ICS file using string builder
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(+330)));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+660)));
str.AppendLine("LOCATION: Main Office");
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");
//sending a mail with attachment ICS file.
SmtpClient client = new SmtpClient();
string user = "your emai";
string pwd = "password";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = ; //smtp server pot
client.Credentials = new System.Net.NetworkCredential(user, pwd);
client.Host = ""; //Host server
client.EnableSsl = true;
System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
contype.Parameters.Add("method", "Request");
contype.Parameters.Add("name", "Apointment.ics");
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
msg.AlternateViews.Add(alternateView);
client.Send(msg);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
--Happy coding :)
0 Comment(s)