将文件附加到iCalendar

时间:2015-03-17 16:36:27

标签: c# .net icalendar mailmessage alternateview

我通过SMTP(使用下面的代码)正确发送iCalendar会议请求,但是当我尝试附加文件时,该文件不会作为iCalendar的一部分出现。在Outlook中打开后保存.ics时,整个文件数据已被删除。

这是我正在使用的代码:

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new System.Net.Mail.MailAddress("test1@test.com", "test1");
msg.To.Add(new System.Net.Mail.MailAddress("test2@test.com", "test2"));
msg.Subject = "Subject1";
msg.Body = "Body line 1\r\nBody line 2\r\nBody line 3";

System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
ct.Parameters.Add("method", "REQUEST");
ct.Parameters.Add("name", "meeting.ics");

System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine("BEGIN:VCALENDAR");

sb.AppendLine("PRODID:-/Microsoft Corporation//Outlook 15.0 MIMEDIR//EN");
sb.AppendLine("VERSION:2.0");
sb.AppendLine("METHOD:REQUEST");
sb.AppendLine("X-MS-OLK-FORCEINSPECTOROPEN:TRUE");

sb.AppendLine("BEGIN:VEVENT");

string file = "D:\\LoadedDate.xlsx";
string filename = Path.GetFileName(file);

sb.Append("ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=");
sb.Append(filename).Append(":").AppendLine(Convert.ToBase64String(File.ReadAllBytes(file), Base64FormattingOptions.InsertLineBreaks));

foreach (System.Net.Mail.MailAddress to in msg.To)
{
    sb.AppendLine(String.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", String.IsNullOrEmpty(to.DisplayName) ? to.Address : to.DisplayName, to.Address));
}
sb.AppendLine("CLASS:PUBLIC");
sb.Append("CREATED:").AppendLine(DateTime.Now.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
sb.Append("DESCRIPTION:").Append(msg.Body.Replace("\r\n", "\\n")).Append("\\n <<").Append(filename).AppendLine(">> \\n");

        string dt = DateTime.Now.AddHours(1).ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z");
        sb.AppendLine("DTSTART:" + dt);
        sb.AppendLine("DTSTAMP:" + dt);
        sb.AppendLine("DTEND:" + DateTime.Now.AddHours(5).ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
        sb.AppendLine("LAST-MODIFIED:");
        sb.Append("LOCATION:").AppendLine("Location1");
        sb.AppendLine(String.Format("ORGANIZER;CN=\"{0}\":mailto:{0}", msg.From.Address));
        sb.AppendLine("PRIORITY:5");
        sb.AppendLine("SEQUENCE:0");
        sb.Append("SUMMARY;LANGUAGE=en-gb:").AppendLine(msg.Subject);
        sb.AppendLine("TRANSP:OPAQUE");

        // UID should be unique.
        sb.Append("UID:").AppendLine(Guid.NewGuid().ToString());
        sb.Append("X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\\n");
        sb.Append("<HTML>\\n").Append("<HEAD>\\n").Append("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html\\; charset=iso-8859-1\">\\n").Append("<META NAME=\"Generator\" CONTENT=\"MS Exchange Server version 14.03.0162.000\">\\n");
        sb.Append("<TITLE>").Append(msg.Subject).Append("</TITLE>\\n");
        sb.Append("</HEAD>\\n").Append("<BODY>\\n").Append("<!--Converted from text/rtf format -->\\n\\n");
        sb.Append("<P DIR=LTR><SPAN LANG=\"en-gb\"><FONT FACE=\"Calibri\">").Append(msg.Body.Replace("\r\n", "</FONT></SPAN></P>\\n\\n<P DIR=LTR><SPAN LANG=\"en-gb\"><FONT FACE=\"Calibri\">")).Append("</FONT></SPAN></P>\\n\\n");
        sb.Append("<P DIR=LTR><SPAN LANG=\"en-gb\"><FONT FACE=\"Arial\" SIZE=2 COLOR=\"#000000\"> &lt\\;&lt\\;").Append(filename).Append("&gt\\;&gt\\; </FONT></SPAN></P>\\n\\n");
        sb.Append("</BODY>\\n").AppendLine("</HTML>");

        sb.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY");
        sb.AppendLine("X-MICROSOFT-CDO-IMPORTANCE:1");
        sb.AppendLine("X-MICROSOFT-DISALLOW-COUNTER:FALSE");
        sb.AppendLine("X-MS-OLK-AUTOFILLLOCATION:FALSE");
        sb.AppendLine("X-MS-OLK-AUTOSTARTCHECK:FALSE");
        sb.AppendLine("X-MS-OLK-CONFTYPE:0");
        sb.AppendFormat("X-MS-OLK-SENDER;CN=\"{0}\":mailto:{0}", msg.From.Address).AppendLine();

        sb.AppendLine("STATUS:TENTATIVE");
        sb.AppendLine("BEGIN:VALARM");
        sb.AppendLine("TRIGGER:-PT15M");
        sb.AppendLine("ACTION:DISPLAY");
        sb.AppendLine("DESCRIPTION:Reminder");
        sb.AppendLine("END:VALARM");
        sb.AppendLine("END:VEVENT");

        sb.AppendLine("END:VCALENDAR");

        System.Net.Mail.AlternateView av = System.Net.Mail.AlternateView.CreateAlternateViewFromString(sb.ToString(), ct);

        msg.AlternateViews.Add(av);

        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mailserver");
        client.Send(msg);

我已经看过了iCalendars的RFC(http://tools.ietf.org/html/rfc5545),我想我已根据规范所说的做了一切。我猜测文件的读取方式存在问题(Convert.ToBase64String位),或者我在备用视图中遗漏了某些内容(我看到其他人添加了多个视图)。

我尝试过的事情:

  • 替换Convert.ToBase64String(File.ReadAllBytes(file), Base64FormattingOptions.InsertLineBreaks) Convert.ToBase64String(File.ReadAllBytes(文件), Base64FormattingOptions.None)。
  • 使用System.Text.Encoding 将文件转换为BASE64(没有成功)。
  • 直接将文件附加到电子邮件中(使用 MailMessage.Attachments),但这只会使电子邮件显示为 普通电子邮件。

我还看了一下sourceforge上的DDay.iCal项目(http://sourceforge.net/projects/dday-ical/),但我无法弄清楚在附加文件时它是如何工作的。

我对此的一个要求是文件必须嵌入/附加到电子邮件中,遗憾的是我无法将其添加为URI。

有人可以帮忙吗?

更新:根据arnaudq的建议,我已经实现了将RFC包含在75个字符中的行。生成的MIME消息如下所示:

BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 15.0 MIMEDIR//EN
VERSION:2.0
METHOD:REQUEST
X-MS-OLK-FORCEINSPECTOROPEN:TRUE
BEGIN:VEVENT
ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=test.txt:U0ZMb2dObwlTRkxvYWR
 lZERhdGUNCjkxNzY3NC8xCTI3LzExLzIwMTIgMTg6MzANCjkxMjIwNS8xCTI3LzExLzIwMTIgM
 Tg6MzANCjkxMjI0Ni8xCTI3LzExLzIwMTIgMTg6MzANCjkxMjI1Mi8xCTI3LzExLzIwMTIgMTg
 6MzANCjkxMjQyMS8xCTI3LzExLzIwMTIgMTg6MzANCjkxMjQyMi8xCTI3LzExLzIwMTIgMTg6M
 zANCjkxNTMyMS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTQzNS8xCTI3LzExLzIwMTIgMTg6MzA
 NCjkxNTU5OS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNjc3NC8xCTI3LzExLzIwMTIgMTg6MzANC
 jkxNjk1OS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNjk2MC8xCTI3LzExLzIwMTIgMTg6MzANCjk
 xNzM2Ny8xCTI3LzExLzIwMTIgMTg6MzANCjkxNzQzNC8xCTI3LzExLzIwMTIgMTg6MzANCjkxN
 DczMS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDczMi8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc
 0My8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc0NC8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc0N
 S8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc0Ni8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc2MS8
 xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc2Mi8xCTI3LzExLzIwMTIgMTg6MzANCjkxNDc2My8xC
 TI3LzExLzIwMTIgMTg6MzANCjkxNTYzNS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTYzOC8xCTI
 3LzExLzIwMTIgMTg6MzANCjkxNTY0MC8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTY0MS8xCTI3L
 zExLzIwMTIgMTg6MzANCjkxNTY1OS8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTc3Ni8xCTI3LzE
 xLzIwMTIgMTg6MzANCjkxNTc3Ny8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTc3OC8xCTI3LzExL
 zIwMTIgMTg6MzANCg==
ATTENDEE;CN="Test 1";RSVP=TRUE:mailto:test1@test.com
CLASS:PUBLIC
CREATED:20150318T095735Z
DESCRIPTION:Body line 1
 Body line 2
 Body line 3
 <<test.txt>> 
DTSTART:20150318T105735Z
DTSTAMP:20150318T105735Z
DTEND:20150318T145735Z
LAST-MODIFIED:
LOCATION:Location1
ORGANIZER;CN="test2@test.com":mailto:test2@test.com
PRIORITY:5
SEQUENCE:0
SUMMARY;LANGUAGE=en-gb:Subject1
TRANSP:OPAQUE
UID:40306717-c29a-42d1-b03e-0240a93c2ea2
X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//E
 N"><HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html\; charse
 t=iso-8859-1"><META NAME="Generator" CONTENT="MS Exchange Server version 1
 4.03.0162.000"><TITLE>Subject1</TITLE></HEAD><BODY><!--Converted from text
 /rtf format --><P DIR=LTR><SPAN LANG="en-gb"><FONT FACE="Calibri">Body lin
 e 1</FONT></SPAN></P><P DIR=LTR><SPAN LANG="en-gb"><FONT FACE="Calibri"></
 FONT></SPAN></P>Body line 2</FONT></SPAN></P><P DIR=LTR><SPAN LANG="en-gb"
 ><FONT FACE="Calibri"></FONT></SPAN></P>Body line 3<P DIR=LTR><SPAN LANG="
 en-gb"><FONT FACE="Arial" SIZE=2 COLOR="#000000"> &lt\;&lt\;test.txt&gt\;&
 gt\; </FONT></SPAN></P></BODY></HTML>
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
X-MICROSOFT-CDO-IMPORTANCE:1
X-MICROSOFT-DISALLOW-COUNTER:FALSE
X-MS-OLK-AUTOFILLLOCATION:FALSE
X-MS-OLK-AUTOSTARTCHECK:FALSE
X-MS-OLK-CONFTYPE:0
STATUS:TENTATIVE
BEGIN:VALARM
TRIGGER:-PT15M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR

不幸的是,这仍然不起作用,并且文件(在这种情况下是一个简单的纯文本文件)没有通过Outlook中的日历条目。

真正有趣的是手动将上述MIME邮件保存到文件并重命名为.ics然后打开它 正确显示附加文件。这让我觉得我发送邮件的方式有问题,而不是iCalendar标记。

任何想法有什么不对?

3 个答案:

答案 0 :(得分:2)

@paul,我做了以下更改,对我来说工作正常。我需要在所有电子邮件客户端上验证此修复程序。我测试了MS Outlook 2013,ios,MS Outlook 2010及其正常工作。

MailMessage msg = new MailMessage();
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
Stream stream = new MemoryStream(attachment.Bytes);// Bytes of file
LinkedResource resource = new LinkedResource(stream);                                                               
resource.ContentId = attachment.Name.Replace(".", "") + DateTime.Now.Ticks.ToString();
resource.ContentType.Name = attachment.Name;//Name of file              
resource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;                               
alternate.LinkedResources.Add(resource);
msg.AlternateViews.Add(alternate);

我不修改.ics文件以添加 ATTACH 属性(ATTACH; ENCODING = BASE64; VALUE = BINARY; X-FILENAME =)

答案 1 :(得分:2)

String iCall = CreateICal();


            System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
            ct.Parameters.Add("charset", @"utf-8");
            ct.Parameters.Add("method", "REQUEST");
            AlternateView avCal = AlternateView.CreateAlternateViewFromString(iCall, ct);



            System.Net.Mime.ContentType cthtml = new System.Net.Mime.ContentType("text/html");
            cthtml.Parameters.Add("charset", @"utf-8");
            AlternateView avHtml = AlternateView.CreateAlternateViewFromString(this.mHTML, cthtml);

            mail.AlternateViews.Add(avHtml);
            mail.AlternateViews.Add(avCal);


            foreach (LinkedResource resource in arrattach)
            {
                avHtml.LinkedResources.Add(resource);
            }

            client.Send(mail);

答案 2 :(得分:1)

首先,当你使用换行符时,看起来你没有使用iCalendar所期望的那种换行符。简而言之,第一个之后的每一行应该以空格字符为前缀,并且行的长度应小于75个八位字节。见https://tools.ietf.org/html/rfc5545#section-3.1

(一般来说,对于这种类型的互操作问题,向我们展示最终结果MIME消息比用于生成它的代码更有用)

然后,据我所知,Outlook更喜欢在包含iCalendar流和不同mime部分中的附件的multipart / related中传输附件。有关示例,请参阅https://tools.ietf.org/html/rfc6047#section-4.6

最后,您可能想尝试从Outlook发送带附件的邀请,并查看它发送的MIME邮件是如何发送的。