C# iCal 邀请未正确显示在 Outlook 中

发布于 2024-12-25 19:21:58 字数 2216 浏览 2 评论 0原文

我的一个客户无法看到与我相同格式的电子邮件。我使用的是 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);
    }

I have a client that cannot see the same format of the emails I have. I am on Outlook 2007 and they are on a mix of 2003, 2007, and 2010. The invite shows up fine for me, but all they see is a normal email and all of the HTML markup shows, instead of the email being formatted.

Here is what I have right now:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南笙 2025-01-01 19:21:58

我花了很长时间试图弄清楚这个问题,最后发现这个对我有用(在 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;
        }

I spent ages trying to figure it out and landed on this which works for me (tested in Outlook 2007 and 2010).

Note: I've not tried cancel yet, this only works for Request.

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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文