C#iCal邀请在Outlook中不显示

时间:2012-01-06 14:34:00

标签: c# outlook icalendar

我的客户端无法看到我所拥有的电子邮件格式。我在Outlook 2007上,他们在2003年,2007年和2010年混合使用。邀请对我来说很好,但他们看到的只是一个普通的电子邮件和所有的HTML标记显示,而不是格式化的电子邮件。

这就是我现在所拥有的:

    protected void SendMessage(MailMessage message, bool cancel)
    {
        AlternateView av = GetInviteContent(cancel);
        message.AlternateViews.Add(av);

        base.SendMessage(message);
    }

    private AlternateView GetInviteContent(bool cancel)
    {
        StringBuilder str = new StringBuilder();
        str.AppendLine("BEGIN:VCALENDAR");
        str.AppendLine("VERSION:2.0");
        if(cancel)
            str.AppendLine("METHOD:CANCEL");
        else
            str.AppendLine("METHOD:REQUEST");
        str.AppendLine("BEGIN:VEVENT");
        if(cancel)
            str.AppendLine("STATUS:CANCELLED");
        str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", StartTime));
        str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", EndTime));
        str.AppendLine(string.Format("LOCATION:{0}", Location));
        str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
        str.AppendLine(string.Format("DESCRIPTION:{0}", Body));
        str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", Body));
        str.AppendLine(string.Format("SUMMARY:{0}", Subject));
        str.AppendLine(string.Format("ORGANIZER;CN={0}:MAILTO:{1}", From.Email, From.FormalName));

        foreach (var to in To)
        {
            str.AppendLine(string.Format("ATTENDEE;CN={0};ROLE=REQ-PARTICIPANT:MAILTO:{1}", to.FormalName, to.Email));
        }

        str.AppendLine("BEGIN:VALARM");
        str.AppendLine(string.Format("TRIGGER:-PT{0}M", Reminder.Minutes));
        str.AppendLine("ACTION:DISPLAY");
        str.AppendLine("DESCRIPTION:Reminder");
        str.AppendLine("END:VALARM");
        str.AppendLine("END:VEVENT");
        str.AppendLine("END:VCALENDAR");
        System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
        if(cancel)
            ct.Parameters.Add("method", "CANCEL");
        else
            ct.Parameters.Add("method", "REQUEST");
        return AlternateView.CreateAlternateViewFromString(str.ToString(), ct);
    }

1 个答案:

答案 0 :(得分:2)

我花了很多年的时间试图解决这个问题,然后找到适用于我的版本(在Outlook 2007和2010中测试过)。

注意:我还没有尝试取消,这仅适用于请求。

public MailMessage CreateMailMessage(string to, string from, String subject, String body, string iCal)
        {
            MailMessage message = new MailMessage(from, to);
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = false;

            ContentType calType = new ContentType("text/calendar");
            //NOTE MUST MATCH iCal Method for RFC2445
            calType.Parameters.Add("method", "REQUEST");

            AlternateView calendarView = AlternateView.CreateAlternateViewFromString(iCal, calType);
            calendarView.TransferEncoding = TransferEncoding.Base64;

            message.AlternateViews.Add(calendarView);

            return message;
        }