在Sendgrid中附加ics文件

问题描述:

如何在C#中使用Sendgrid发送日历邀请?在Sendgrid中附加ics文件

我可以将ics文件附加到邮件中,但是当我下载附件时,出现“无效日历文件”错误。

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:[email protected] DTSTAMP:20141014T212813Z ORGANIZER;CN=\"[email protected]\";SENT-BY=\"MAILTO:[email protected]\";LANGUAGE=se:MAILTO:[email protected] ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Fessy M;X-NUM-GUESTS=0:MAILTO:[email protected] 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"); 
+0

你们是不是要发送日历通过电子邮件邀请或从网页上下载? –

如果你试图发送一个日历,你需要做到以下几点:

// 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 
+0

无法添加上面的附件。文档说你必须使用'AddAttachment()'方法。但是,然后,我无法设置MimeType – Belicosus

+0

此答案似乎使用SMTPClient和System.Net.Mail类使用SendGrid凭据发送电子邮件。原来的问题似乎是使用SendGrid C#客户端API库之一。这就是为什么Belicosus说文档使用AddAttachment()并且Black Frog提供的答案使用_Message.Attachments.Add() –

有几件事情。

首先CalendarContent看起来不合法,它没有任何换行符。

首先尝试:

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:[email protected] 
DTSTAMP:20141014T212813Z 
ORGANIZER;CN=\"[email protected]\";SENT-BY=\"MAILTO:[email protected]\";LANGUAGE=se:MAILTO:[email protected] 
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Fessy M;X-NUM-GUESTS=0:MAILTO:[email protected] 
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"; 

如果不工作,它会出现以下不遵循RFC 5545但我不看好这一点,RFC可能会造成混淆:

ORGANIZER;CN=\"[email protected]\";SENT-BY=\"MAILTO:[email protected]\";LANGUAGE=se:MAILTO:[email protected] 
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Fessy M;X-NUM-GUESTS=0:MAILTO:[email protected] 

可能是:

ORGANIZER;LANGUAGE=se:SENT-BY=\"mailto:[email protected]\":mailto:[email protected] 
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Fessy M;X-NUM-GUESTS=0:mailto:[email protected]