【发布时间】:2017-07-13 09:40:00
【问题描述】:
我想向 Outlook 以及 gmail/yahoo 等非 Outlook 客户端发送一封带有日历邀请/约会的电子邮件。我的应用程序托管在 Azure 上,我正在使用 SendGrid 发送电子邮件。电子邮件部分工作得很好,但我还没有找到任何适用于 Outlook 和其他电子邮件客户端的完全有效的解决方案。这是我用来发送电子邮件的代码 sn-p:
var client = new SendGridClient(this.apiKey);
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(
new EmailAddress(Sender, SenderName),
recipients, subject, textcontent, htmlcontent);
if (isMeetingRequest)
{
Attachment attachment = new Attachment();
attachment.Filename = "calendar.ics";
attachment.Content = htmlcontent;
attachment.Type = "text/calendar";
msg.Attachments = new List<Attachment> { attachment };
}
await client.SendEmailAsync(msg);
htmlContent 来自另一个编码 sn-p,它形成了日历邀请字符串:
private static string MeetingRequestString(string from, List<string> toUsers, string subject, string desc, DateTime startTime, DateTime endTime)
{
StringBuilder str = new StringBuilder();
str.AppendLine("BEGIN:VCALENDAR");
str.AppendLine("PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN");
str.AppendLine("VERSION:2.0");
str.AppendLine(string.Format("METHOD:REQUEST"));
str.AppendLine("BEGIN:VEVENT");
str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmss}", DateTime.Now));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime));
str.AppendLine(string.Format("UID:{0}", Guid.NewGuid().ToString()));
str.AppendLine(string.Format("DESCRIPTION:{0}", desc.Replace("\n", "<br>")));
str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", desc.Replace("\n", "<br>")));
str.AppendLine(string.Format("SUMMARY:{0}", subject));
str.AppendLine(string.Format("ORGANIZER;CN=\"{0}\":MAILTO:{1}", from, from));
str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", string.Join(",", toUsers), string.Join(",", toUsers)));
str.AppendLine("BEGIN:VALARM");
str.AppendLine("TRIGGER:-PT15M");
str.AppendLine("ACTION:DISPLAY");
str.AppendLine("DESCRIPTION:Reminder");
str.AppendLine("END:VALARM");
str.AppendLine("END:VEVENT");
str.AppendLine("END:VCALENDAR");
return str.ToString();
}
这似乎不起作用。任何指针?
【问题讨论】:
-
您解决了这个问题吗,需要进一步的帮助吗?
-
@BruceChen 它适用于 Gmail,但在 Outlook 中,我在电子邮件中看到了邀请.ics 附件(电子邮件没有接受、拒绝等的日历选项)但我在 Outlook 日历中没有看到.当我尝试从电子邮件打开invite.ics 时,它会显示“将此 Internet 日历添加到 Outlook”对话框,其中包含一些警告内容。如果我单击是,它会添加到日历中。因此,默认情况下,邀请不会添加到 Outlook 的日历中。我正在使用 Office 365。
标签: c# email azure outlook sendgrid