简体   繁体   中英

Attach ics file in Sendgrid

How to send Calendar Invite using Sendgrid in C#?

I am able to attach the ics file to the mail, but when I download the attachement I get the error as "Invalid Calendar File".

string CalendarContent = "BEGIN:VCALENDAR VERSION:2.0 PRODID:-//Meeter/meeter//NONSGML v1.0//EN CALSCALE:GREGORIAN METHOD:REQUEST BEGIN:VEVENT DTSTART:20141018T203000Z DTEND:20141018T210000Z UID:20141015T002813-223788868@meeter.com DTSTAMP:20141014T212813Z ORGANIZER;CN=\"snaggs@gmail.com\";SENT-BY=\"MAILTO:someapp@gmail.com\";LANGUAGE=se:MAILTO:snaggs@gmail.com ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Fessy M;X-NUM-GUESTS=0:MAILTO:snaggs2@gmail.com DESCRIPTION:dddd mandrill LOCATION:dddddd mandrill SUMMARY:Can I lay low? Cook some yay-yo 2 TRANSP:OPAQUE SEQUENCE:0 STATUS:CONFIRMED END:VEVENT END:VCALENDAR";

using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(CalendarContent )))
                    {
                        _Message.AddAttachment(ms, "meeting.ics");
                    }
                    var Header = new Dictionary<string, string>();
                    Header.Add("Content-Type", "text/calendar");

If you are trying to send a calendar you will need to do the following:

// you already have the _Message & CalendarContent created above

// first thing, convert calendar content to byte array and then stream
byte[] calendarBytes = Encoding.UTF8.GetBytes(CalendarContent.ToString());
Stream calendarStream = new MemoryStream(calendarBytes);

// them create a attachment for your mail message
Attachment calendarAttachment = new Attachment(calendarStream, "calendar.ics", "text/calendar");
calendarAttachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;  

// now attach the calendar to your Mail Message
_Message.Attachments.Add(calendarAttachment);

// now send the message off

A few things.

First you CalendarContent doesn't look valid, it doesn't have any line breaks in it.

First try:

string CalendarContent = @"BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Meeter/meeter//NONSGML v1.0//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART:20141018T203000Z
DTEND:20141018T210000Z
UID:20141015T002813-223788868@meeter.com
DTSTAMP:20141014T212813Z
ORGANIZER;CN=\"snaggs@gmail.com\";SENT-BY=\"MAILTO:someapp@gmail.com\";LANGUAGE=se:MAILTO:snaggs@gmail.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Fessy M;X-NUM-GUESTS=0:MAILTO:snaggs2@gmail.com
DESCRIPTION:dddd mandrill
LOCATION:dddddd mandrill
SUMMARY:Can I lay low? Cook some yay-yo 2 
TRANSP:OPAQUE
SEQUENCE:0 
STATUS:CONFIRMED
END:VEVENT
END:VCALENDAR";

If that doesn't work, it appears the following doesn't follow RFC 5545 but I'm not positive about this, the RFC can be confusing:

ORGANIZER;CN=\"snaggs@gmail.com\";SENT-BY=\"MAILTO:someapp@gmail.com\";LANGUAGE=se:MAILTO:snaggs@gmail.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Fessy M;X-NUM-GUESTS=0:MAILTO:snaggs2@gmail.com

might be:

ORGANIZER;LANGUAGE=se:SENT-BY=\"mailto:someapp@gmail.com\":mailto:snaggs@gmail.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Fessy M;X-NUM-GUESTS=0:mailto:snaggs2@gmail.com

Try this

// Create your calendar string

string lsCalendar = CreateCalendar(loMessage, fsUID, fdtStartDateTime, fdtEndDateTime, fdtTimeStamp, fsTimeZone, fsLocation, fsRecurType, fsWeekdays, fsAction);
byte[] calendarBytes = Encoding.UTF8.GetBytes(lsCalendar.ToString());
Stream calendarStream = new MemoryStream(calendarBytes);
loMessage.AddAttachment(calendarStream, "invite.ics");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM