using System; using System.Net.Mail; using System.Net.Mime; public class CalendarEvent { public string Subject { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public string Location { get; set; } public string Description { get; set; } public void AddToCalendar(string recipientEmail) { // Create a new MailMessage MailMessage message = new MailMessage(); // Set the sender and recipient email addresses message.From = new MailAddress("your_email@example.com"); message.To.Add(recipientEmail); // Set the subject of the email message.Subject = Subject; // Create an alternate view to attach the iCalendar (.ics) file AlternateView calendarView = AlternateView.CreateAlternateViewFromString(GetCalendarString(), null, MediaTypeNames.Text.Calendar); calendarView.TransferEncoding = TransferEncoding.SevenBit; message.AlternateViews.Add(calendarView); // Send the email SmtpClient client = new SmtpClient("smtp.example.com", 587); // Replace with your SMTP server details client.Credentials = new System.Net.NetworkCredential("your_email@example.com", "your_password"); // Replace with your email credentials client.EnableSsl = true; client.Send(message); } private string GetCalendarString() { // Create an iCalendar (.ics) string based on the event details string calendarString = string.Format("BEGIN:VCALENDAR\r\n" + "VERSION:2.0\r\n" + "PRODID:-//Your Company//NONSGML Event Calendar//EN\r\n" + "BEGIN:VEVENT\r\n" + "UID:{0}\r\n" + "DTSTAMP:{1:yyyyMMddTHHmmssZ}\r\n" + "DTSTART:{2:yyyyMMddTHHmmssZ}\r\n" + "DTEND:{3:yyyyMMddTHHmmssZ}\r\n" + "LOCATION:{4}\r\n" + "DESCRIPTION:{5}\r\n" + "SUMMARY:{6}\r\n" + "END:VEVENT\r\n" + "END:VCALENDAR", Guid.NewGuid().ToString(), DateTime.UtcNow, StartDate, EndDate, Location, Description, Subject); return calendarString; } }