0

我已经为发送给用户的电子邮件创建了一个 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);
4

1 回答 1

0

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

例如,您可以在此处阅读 Microsoft 将 iCal 转换为约会项目的开放规范。

于 2018-11-23T20:40:00.663 回答