使用ics会议预约向Outlook发送电子邮件

时间:2014-03-29 17:33:20

标签: c# email icalendar appointment

我想向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测试它)它显示了正文,位置,数据,它显示了日历,但在我看到的邮件中的日历上方"无法在日历上找到会议" "接受","拒绝""按钮被禁用!

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

2 个答案:

答案 0 :(得分:11)

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

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

Content-class: urn:content-classes:calendarmessage

所以将此添加到您的代码中它应该可以正常工作

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

答案 1 :(得分:1)

我认为你在msg.From和msg.To

中都使用了相同的电子邮件ID
msg.From = new MailAddress("xxxxx@xyz.com", "ABC");
msg.To.Add(new MailAddress("yyyy@xyz.com", "BCD"));

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