生成并向Outlook发送iCal事件

时间:2015-06-05 08:29:40

标签: c# outlook gmail icalendar

我正在使用DDay.iCal(1.0.2.575,来自nuget)来生成电子邮件事件。 我使用以下代码生成事件:

iCalendar iCal = new iCalendar
{
    Method = "PUBLISH",
    Version = "2.0"
};
//REQUEST for update
Event calendarEvent = iCal.Create<Event>();
calendarEvent.Summary = "Test";
calendarEvent.Start = new iCalDateTime(new DateTime(2015, 7, 1, 10, 0, 0));
calendarEvent.Duration = new TimeSpan(0, 45, 0);
calendarEvent.Description = "Test";
calendarEvent.Location = "Test";
calendarEvent.IsAllDay = false;
calendarEvent.UID = Guid.NewGuid().ToString();
calendarEvent.Organizer = new Organizer("some@address.com");

iCalendarSerializer serializer = new iCalendarSerializer(iCal);
string eventCode = serializer.SerializeToString(calendarEvent);

然后我使用以下代码发送它

SmtpClient smtpClient = new SmtpClient("smtp.someaddress.com");
MailMessage message = new MailMessage
{
    From = new MailAddress("some@address.com", "Someone"),
    Subject = "Test",
    Body = "Test"
};
System.Net.Mail.Attachment attachment = System.Net.Mail.Attachment.CreateAttachmentFromString(eventCode, "test.ics");
attachment.ContentType = new System.Net.Mime.ContentType("text/calendar");
attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
message.CC.Add(new MailAddress("some@adderss.com"));
message.Attachments.Add(attachment);
smtpClient.Send(message);

,输出

BEGIN:VEVENT
DESCRIPTION:Test
DTEND:20150701T104500
DTSTAMP:20150605T082404Z
DTSTART:20150701T100000
DURATION:PT45M
LOCATION:Test
ORGANIZER:mailto:some@address.com
SEQUENCE:0
SUMMARY:Test
UID:dd175008-a4be-4fcf-a153-baff29ef5b8a
END:VEVENT

但是outlook 2010只是说The operation failed.并且gmail显示了一个日历符号但是如果我点击它就没有任何反应,如果我点击附加的.ics文件它也不知道该怎么做..

我缺少什么使这项工作?

1 个答案:

答案 0 :(得分:2)

我发现错误会将行string eventCode = serializer.SerializeToString(calendarEvent);更改为string eventCode = serializer.SerializeToString(iCal);并且有效!