我需要向 Web 应用程序的用户发送提醒。
为此,我使用 nuget 包中的 iCal.Net。按照有关使用的说明,我可以发送带有包含 ics 文件的附件的电子邮件。
我的第一个问题是:是否可以将消息作为约会而不是附件发送,这样用户就可以避免打开它然后保存日期?
第二:是否可以将事件描述格式化为 html 并使其在 Outlook Office 客户端和 Chrome 日历上都可以使用?在这些客户端上,我看到带有所有 html 标签的纯文本表示。我还在 Windows 10 日历上进行了测试,其中事件描述正确显示为 html。我还需要设置更多吗?
这是我的日历字符串生成
var now = DateTime.Now;
var later = scadenza.Value;
var e = new Event
{
DtStart = new CalDateTime(later),
DtEnd = new CalDateTime(later),
Description = mailText,
IsAllDay = true,
Created = new CalDateTime(DateTime.Now),
Summary = $"Reminder for XYZ",
};
var attendee = new Attendee
{
CommonName = "…..",
Rsvp = true,
Value = new Uri("mailto:" + ConfigurationManager.AppSettings["ReminderReceiver"])
};
e.Attendees = new List<IAttendee> { attendee };
var calendar = new Calendar();
calendar.Events.Add(e);
var serializer = new CalendarSerializer(new SerializationContext());
var icalString = serializer.SerializeToString(calendar);
这是用于发送提醒;请注意,如果我不“附加” ics,则备用视图似乎无效。
var message = new MailMessage();
message.To.Add(new MailAddress(receiverRecipient));
message.From = new MailAddress(senderRecipient);
message.Subject = messageTitle;
message.Body = "The attachment contains the reminder";
message.IsBodyHtml = true;
var smtpClient = new SmtpClient(ConfigurationManager.AppSettings["SmtpSrv"], Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]));
var cred = new NetworkCredential(ConfigurationManager.AppSettings["SmtpUser"], ConfigurationManager.AppSettings["SmtpPwd"]);
smtpClient.Credentials = cred;
System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
contype.Parameters.Add("method", "REQUEST");
contype.Parameters.Add("name", "Scadenza.ics");
AlternateView avCal = AlternateView.CreateAlternateViewFromString(icalString, contype);
message.AlternateViews.Add(avCal);
message.Attachments.Add(new Attachment(GenerateStreamFromString(icalString), contype));
smtpClient.Send(message);