Skip to content

Instantly share code, notes, and snippets.

@Stayrony
Last active April 25, 2023 13:45
Show Gist options
  • Save Stayrony/1f885bddd7da88f414f6205a138516a6 to your computer and use it in GitHub Desktop.
Save Stayrony/1f885bddd7da88f414f6205a138516a6 to your computer and use it in GitHub Desktop.
Create iCal ics Files in C# ASP.NET Core
public class CalendarNotificationModel
{
public string Name { get; set; }
public IEnumerable<Attendee> Attendees { get; set; }
public DateTime StartDateTime { get; set; }
public string CreatorEmail { get; set; }
public string Location { get; set; }
public string Description { get; set; }
}
public class Attendee
{
public string AttendeeName { get; set; }
public string AttendeeEmail { get; set; }
}
public async Task<SendResponse> CreateCalendarEventAsync(
CalendarNotificationModel calendarNotificationModel)
{
var attendees = calendarNotificationModel.Attendees.Select(x => new Ical.Net.DataTypes.Attendee()
{
CommonName = x.AttendeeName,
ParticipationStatus = "REQ-PARTICIPANT",
Rsvp = true,
Value = new Uri($"mailto:{x.AttendeeEmail}")
}).ToList();
var e = new CalendarEvent
{
Summary = calendarNotificationModel.Name,
IsAllDay = true,
Organizer = new Organizer()
{
CommonName = "Your app name",
Value = new Uri($"mailto:{[email protected]}")
},
Attendees = attendees,
Start = new CalDateTime(calendarNotificationModel.StartDateTime.Date),
Transparency = TransparencyType.Transparent,
Location = calendarNotificationModel.Location,
Description = calendarNotificationModel.Description
};
var calendar = new Calendar();
calendar.Events.Add(e);
var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(calendar);
var bytesCalendar = Encoding.ASCII.GetBytes(serializedCalendar);
MemoryStream ms = new MemoryStream(bytesCalendar);
using (ms)
{
ms.Position = 0;
var fileName = calendarNotificationModel.Name.Replace(' ', '_');
var result = await _emailProviderService.SendAsync(_sendGridOptions.EmailFrom,
calendarNotificationModel.CreatorEmail,
null,
null,
"Calendar event", //Subject,
"Your future event details.", //Template
new List<Attachment>
{
new Attachment()
{
Filename = $"{fileName}.ics",
Data = ms,
ContentType = "text/calendar",
IsInline = false
}
},
true);
return result;
}
}
public class EmailProviderService : IEmailProviderService
{
private readonly SendGridOptions _sendGridOptions;
public EmailProviderService(IOptions<SendGridOptions> sendGridOptions)
{
_sendGridOptions = sendGridOptions.Value;
FluentEmail.Core.Email.DefaultSender = new SendGridSender(_sendGridOptions.ApiKey);
FluentEmail.Core.Email.DefaultRenderer = new RazorRenderer();
}
public async Task<SendResponse> SendAsync(string from, string to, IList<string> cc, string subject,
string template, bool isHtml = false)
{
return await SendAsync(from, to, cc, null, subject, template, isHtml);
}
public async Task<SendResponse> SendAsync(string from, string to, IList<string> cc, IList<string> bcc,
string subject, string template, IEnumerable<Attachment> attachments, bool isHtml = false)
{
var sendEmail = FluentEmail.Core.Email.From(from)
.To(to)
.Subject(subject)
.Body(template, isHtml);
if (cc != null)
{
var ccList = cc.Select(x => new Address {EmailAddress = x}).ToList();
sendEmail = sendEmail.CC(ccList);
}
if (bcc != null)
{
var bccList = bcc.Select(x => new Address {EmailAddress = x}).ToList();
sendEmail = sendEmail.BCC(bccList);
}
if (attachments != null)
{
sendEmail.Attach(attachments.ToList());
}
return await sendEmail.SendAsync();
}
public interface IEmailProviderService
{
Task<SendResponse> SendAsync(string from, string to, IList<string> cc, IList<string> bcc,
string subject, string template, IEnumerable<Attachment> attachments, bool isHtml = false);
}
public class SendGridOptions
{
public string ApiKey { get; set; }
public string EmailFrom { get; set; }
}
@Stayrony
Copy link
Author

How to create an email with iCal file?
See this post

@ShahRachna
Copy link

Hi I need a some help in this solution.
I implemented the same but having some issues in to running ,like namespace and all are not getting properly so may I have full solution ?(working/running)?

@Stayrony
Copy link
Author

Hi,
Have you ever read this post?
What is the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment