C#如何在电子邮件中创建指向.ICS文件的超链接

时间:2018-11-23 19:28:12

标签: c# asp.net outlook vcalendar

我已经为发送给用户的电子邮件创建了一个calender.ics文件附件。然后,用户可以将约会详细信息添加到他们的日历中(无论他们选择Outlook,Google日历,Yahoo!等)。如果单击附件并下载该文件,则.ics文件的效果很好。但是,我无休止地搜索了一个不错的示例,该示例中存在“添加到日历”超链接,或者没有成功的解决方法。有没有办法在C#中做到这一点?我想念什么吗?这是我在Icalender的第一个步骤,我正在使用StringBuilder()构建.ICS文件。

        var customerMailMessage = new MailMessage(Store.LocationEmail, CustomerEmail);
        customerMailMessage.Subject = "Appointment Request For " + ServiceDate.ToShortDateString();
        customerMailMessage.Body = body.ToString();
        customerMailMessage.IsBodyHtml = true;

        //Start of calender invite
        DateTime ServiceTime = ServiceDate.Date.Add(StartTime.TimeOfDay);
        DateTime DateStart = Convert.ToDateTime(ServiceTime);
        string DateFormat = "yyyyMMddTHHmmssZ";
        string now = DateTime.Now.ToUniversalTime().ToString(DateFormat);
        string Location = string.Format(Store.LocationAddress1 + " " + Store.LocationCity + " " + Store.LocationState + " " + Store.LocationZip);
        string Summary = "Calendar reminder";
        string Description = "Your appointment details on: " + DateStart;
        string FileName = "ServiceAppointment.ics";
        Guid guid = Guid.NewGuid();

        StringBuilder sb = new StringBuilder();
        sb.AppendLine("BEGIN:VCALENDAR");
        sb.AppendLine("VERSION:2.0");
        sb.AppendLine("PRODID:Domain.com");
        sb.AppendLine("CALSCALE:GREGORIAN");
        sb.AppendLine("METHOD:REQUEST");
        sb.AppendLine("BEGIN:VEVENT");
        sb.AppendLine("UID:" + guid);
        sb.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", retailStore.LocationEmail));
        sb.AppendLine(string.Format("ATTENDEE;CN='{0}, {1}';ROLE=REQ-PARTICIPANT;PARTSTAT=TENTATIVE:MAILTO:{2}", CustomerFirstName, CustomerLastName, CustomerEmail));
        sb.AppendLine("DTSTART:" + DateStart.ToUniversalTime().ToString(DateFormat));
        sb.AppendLine("DTSTAMP:" + now);
        sb.AppendLine("SUMMARY: " + Summary);
        sb.AppendLine("LOCATION: " + Location);
        sb.AppendLine("DESCRIPTION:" + Description);
        sb.AppendLine("PRIORITY:5");
        sb.AppendLine("TRANSP:OPAQUE");
        sb.AppendLine("END:VEVENT");
        sb.AppendLine("END:VCALENDAR");

        var calendarBytes = Encoding.UTF8.GetBytes(sb.ToString());
        System.IO.MemoryStream stream = new System.IO.MemoryStream(calendarBytes);
        Attachment attachment = new Attachment(stream, FileName, "text/calendar");
        customerMailMessage.Attachments.Add(attachment);

        System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
        contype.Parameters.Add("method", "REQUEST");
        contype.Parameters.Add("name", "ServiceAppointment.ics");


        var emailProvider = new EmailProvider();
        emailProvider.Send(customerMailMessage, "Request Appointment - Customer - " + CreatedBySource);

1 个答案:

答案 0 :(得分:0)

简答:邮件正文中的链接不会调用服务器端将ICS文件转换为用户邮箱的日历项的方法;特别是,考虑到提到的每个平台(Exchange,Google Mail,Yahoo Mail等)都有自己的实现方式(无论是通过Web还是客户端)。

例如,您可以阅读Microsoft的《开放规范》,该文档将iCal转换为约会项目here