如何以与Outlook在.NET中相同的方式发送iCalendar

时间:2019-01-11 09:32:49

标签: .net outlook icalendar mailkit mimekit

目前,我可以使用MailKit发送带有ics文件的电子邮件,并使用iCal.net创建附件。这有效,但我只能将其视为附件。当我在Outlook中手动创建约会时,将获得精美的UI。

Outlook Example

以编程方式创建它时,我会得到它。

enter image description here

我想以编程方式创建精美的UI。看来这与ICS无关,因为我是通过Outlook手动创建的ICS文件。并使用该ics文件通过我的应用程序发送,仍然没有成功。

static void Main(string[] args)
    {
        string serializedCalendar = CreateRecurringEvent();
        File.WriteAllText(@"F:\test.ics", serializedCalendar);
        SendMail();
    }

    private static void SendMail()
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Joey Tribbiani", "joey@tribbiani.com"));
        message.To.Add(new MailboxAddress("Monica Geller", "monica@geller.com"));
        message.To.Add(new MailboxAddress("Chandler Muriel Bing", "chandler@murielbing.com"));
        message.Subject = "How you doin?";

        var builder = new BodyBuilder
        {
            TextBody = @"<bodytext>"
        };

        builder.Attachments.Add(@"F:\test.ics");
        message.Body = builder.ToMessageBody();

        using (var client = new SmtpClient())
        {
            client.ServerCertificateValidationCallback = (s, c, h, e) => true;
            client.Connect("<host>", 235236);
            client.Authenticate("<username>", "<password>");
            client.Send(message);
            client.Disconnect(true);
        }
    }

    private static string CreateRecurringEvent()
    {
        DateTime now = DateTime.Now;
        DateTime later = now.AddHours(1);
        var rrule = new RecurrencePattern(FrequencyType.Daily, 1) { Count = 5 };

        var e = new CalendarEvent
        {
            Start = new CalDateTime(now),
            End = new CalDateTime(later),
            RecurrenceRules = new List<RecurrencePattern> { rrule }
        };

        var calendar = new Calendar();
        calendar.Events.Add(e);

        var serializer = new CalendarSerializer();
        return serializer.SerializeToString(calendar);
    }
}

1 个答案:

答案 0 :(得分:1)

以下是创建可与Outlook配合使用的会议请求的方法:

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Joey Tribbiani", "joey@tribbiani.com"));
message.To.Add(new MailboxAddress("Monica Geller", "monica@geller.com"));
message.To.Add(new MailboxAddress("Chandler Muriel Bing", "chandler@murielbing.com"));
message.Subject = "How you doin?";

var mixed = new Multipart ("mixed");
mixed.Add (new TextPart ("plain") {
    ContentTransferEncoding = ContentEncoding.QuotedPrintable,
    Text = "Would you like to join this meeting?"
});

var ical = new TextPart ("calendar") {
    ContentTransferEncoding = ContentEncoding.Base64,
    Text = CreateRecurringEvent (),
};

ical.ContentType.Parameters.Add ("method", "REQUEST");

mixed.Add (ical);

message.Body = mixed;