【问题标题】:Send email to Outlook with ics meeting appointment使用 ics 会议预约向 Outlook 发送电子邮件
【发布时间】:2014-05-09 04:31:03
【问题描述】:

我想向 Outlook 客户端发送一封带有约会\会议 (ICS) 的电子邮件。当用户收到电子邮件时,他应该接受会议邀请,会议自动进入日历,电子邮件被自动删除。

我正在使用此代码:

public void Sendmail_With_IcsAttachment()
{

    MailMessage msg = new MailMessage();
    //Now we have to set the value to Mail message properties

    //Note Please change it to correct mail-id to use this in your application
    msg.From = new MailAddress("xxxxx@xyz.com", "ABC");
    msg.To.Add(new MailAddress("yyyyy@xyz.com", "BCD"));
    msg.CC.Add(new MailAddress("zzzzz@xyz.com", "DEF"));// it is optional, only if required
    msg.Subject = "Send mail with ICS file as an Attachment";
    msg.Body = "Please Attend the meeting with this schedule";

    // Now Contruct the ICS file using string builder
    StringBuilder str = new StringBuilder();
    str.AppendLine("BEGIN:VCALENDAR");
    str.AppendLine("PRODID:-//Schedule a Meeting");
    str.AppendLine("VERSION:2.0");
    str.AppendLine("METHOD:REQUEST");
    str.AppendLine("BEGIN:VEVENT");
    str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+330)));
    str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
    str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+660)));
    str.AppendLine("LOCATION: " + this.Location);
    str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
    str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body));
    str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));
    str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject));
    str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));

    str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address));

    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");

    //Now sending a mail with attachment ICS file.                     
    System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
    smtpclient.Host = "localhost"; //-------this has to given the Mailserver IP

    smtpclient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

    System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
    contype.Parameters.Add("method", "REQUEST"); 
    contype.Parameters.Add("name", "Meeting.ics");
    AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);
    msg.AlternateViews.Add(avCal);
    smtpclient.Send(msg); 
}

邮件已正确发送,但在 Outlook 中(我正在使用 Outlook 2010 对其进行测试)它显示正文、位置、数据,并显示日历,但在邮件中的日历上方我看到“无法找到会议在日历上”和“接受”、“拒绝””按钮被禁用!

我尝试了其他解决方案,并在网上找到了 ex。 DDAY.Ical,但我没有找到任何使用它的示例。

【问题讨论】:

  • 仅供参考,您的 MailMessageSmtpClient 都需要在 using 块中:using (MailMessage msg = new MailMessage()){... using (SmtpClient smtpClient = new SmtpClient()){ ... smtpClient.Send(msg);}}
  • 好的,谢谢,但我不认为这是问题
  • 不管是不是问题,你都应该这样做——我看到邮件在 2 分钟内没有送达,直到垃圾收集器开始清理。
  • 您能否将生成的完整 MIME 消息添加到您的帖子中?
  • 如何获得完整的 MIME 消息?

标签: c# email icalendar appointment


【解决方案1】:

经过大量挖掘,我找到了解决方案:

您需要将电子邮件的内容类别设置为

Content-class: urn:content-classes:calendarmessage

所以把它添加到你的代码中,它应该可以工作

msg.Headers.Add("Content-class", "urn:content-classes:calendarmessage");

【讨论】:

  • 这种情况发生在我将 Exchange 升级到 2010 时。我使用了两个 AlternativeView(1 个用于 HTML,1 个用于日历)。为了解决这个问题,我必须订购它们,以便 HTML 优先 AlternateView HTML = AlternateView.CreateAlternateViewFromString(body, new System.Net.Mime.ContentType("text/html"));msg.AlternateViews.Add(HTML); AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(),contype); msg.AlternateViews.Add(avCal ); 我知道这不是这个实例的问题,只是提到它以防其他人有同样的问题。跨度>
  • 谢谢,我还需要在 DTSTART 和 DTEND 中使用 Utc Date
  • 添加内容类正是它所需要的,感谢分享它
【解决方案2】:

我认为您在 msg.From 和 msg.To 中使用了相同的电子邮件 ID

msg.From = new MailAddress("xxxxx@xyz.com", "ABC");
msg.To.Add(new MailAddress("yyyy@xyz.com", "BCD"));

我也遇到过同样的问题,但在收到不同的电子邮件后得到解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2011-09-28
    相关资源
    最近更新 更多