ical.net
ical.net copied to clipboard
Attachments does not work
I'm trying to attach files to my email, which include even invitation.
I follow this tutorial : https://github.com/rianjs/ical.net/wiki/Working-with-attachments#base64-encoded-attachment
But it didn't work.
var e = new CalendarEvent
{
Summary = booking.Title,
Start = new CalDateTime(start),
End = new CalDateTime(end),
Location = booking.Meta.RoomName,
Description = booking.Meta.Notes,
Organizer = new Organizer(booking.organizer)
};
...
foreach (var attachment in booking.Meta.Attachments)
{
var attachmentContent = _attachmentService.GetStreamAsync(attachment.ResourceId).Result;
using (attachmentContent.Stream)
{
using (var ms = new MemoryStream())
{
attachmentContent.Stream.CopyTo(ms);
var att = new Ical.Net.DataTypes.Attachment(ms.ToArray());
att.Parameters.Add("X-FILENAME", attachmentContent.FileName);
e.Attachments.Add(att);
}
}
}
var calendar = new Calendar();
calendar.Events.Add(e);
calendar.Method = "REQUEST";
var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(calendar);
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
ct.Parameters.Add("method", "REQUEST");
ct.Parameters.Add("charset", "utf-8");
AlternateView avCal = AlternateView.CreateAlternateViewFromString(serializer.SerializeToString(calendar), ct);
...
using (var mess = new MailMessage())
{
foreach (var item in attendees)
{
mess.To.Add(new MailAddress(item.Username));
}
mess.From = new MailAddress(_configuration["Email:Email"]);
mess.Subject = subject;
mess.AlternateViews.Add(avCal);
client.Send(mess);
}
I tested in Outlook 365, gmail, window 10 Mail. I received the invitation, but I didn't receive any attachment
@hung-doan Hi, I meet the same question as you, I would be very grateful if you could help me.
@mengjoel, It has been a long time, I'm not sure how I fixed it. However, you can try a solution in my comments on Stackoverflow: https://stackoverflow.com/a/51981938/2022033 Hopefully, it could help.
@hung-doan This problem was solved with your help.Thank you very much.
I have the same problem as you, Have you solved it?
@
I have the same problem as you, Have you solved it?
I think you can add attachments like this
AlternateView viewHtml = AlternateView.CreateAlternateViewFromString(this.CalendarModel.Body, typeHtml);
typeHtml.Parameters.Add("charset", @"utf-8");
mail.AlternateViews.Add(viewHtml);
Stream stream = new MemoryStream(bytes);
LinkedResource resource = new LinkedResource(stream);
resource.ContentId = DateTime.Now.Ticks.ToString();
resource.ContentType.Name = "researchofadfs.docx";//Name of file
resource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
viewHtml.LinkedResources.Add(resource);
@mengjoel Can I get whole code? Thanks.
@mengjoel a full example would be really help here as well...