ical.net icon indicating copy to clipboard operation
ical.net copied to clipboard

Attachments does not work

Open hung-doan opened this issue 6 years ago • 7 comments

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 avatar Aug 22 '18 10:08 hung-doan

@hung-doan Hi, I meet the same question as you, I would be very grateful if you could help me.

mengjoel avatar Aug 09 '19 02:08 mengjoel

@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.

hungdoan-groove avatar Aug 09 '19 03:08 hungdoan-groove

@hung-doan This problem was solved with your help.Thank you very much.

mengjoel avatar Aug 09 '19 08:08 mengjoel

I have the same problem as you, Have you solved it?

Jasonzhangjs avatar Sep 04 '19 10:09 Jasonzhangjs

@

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 avatar Sep 05 '19 02:09 mengjoel

@mengjoel Can I get whole code? Thanks.

Jasonzhangjs avatar Sep 05 '19 02:09 Jasonzhangjs

@mengjoel a full example would be really help here as well...

almostjulian avatar Oct 15 '19 21:10 almostjulian