Attaching a file to an iCalendar

强颜欢笑 提交于 2019-12-24 02:09:59

问题


I have iCalendar meeting requests sending correctly via SMTP (using the code below), but when I attempt to attach a file, the file does not appear as part of the iCalendar. When saving out the .ics after opening it in outlook, the whole file data has been stripped out.

Here's the code I'm using:

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

I've had a look at the RFC for iCalendars (http://tools.ietf.org/html/rfc5545), and I think I've done everything according to what the spec says. I'm guessing that there is either a problem with the way the file is read in (the Convert.ToBase64String bit), or I'm missing something with the alternate view (I've seen other people adding multiple views).

Things I have tried:

  • Replacing the Convert.ToBase64String(File.ReadAllBytes(file), Base64FormattingOptions.InsertLineBreaks) with Convert.ToBase64String(File.ReadAllBytes(file), Base64FormattingOptions.None).
  • Using System.Text.Encoding to convert the file to BASE64 (without success).
  • Attaching files to the email directly (using the MailMessage.Attachments), but that just makes the email appear as a normal email.

I've also had a look at the DDay.iCal project on sourceforge (http://sourceforge.net/projects/dday-ical/), but I couldn't figure out how that worked when it came to attaching a file.

One requirement I have for this is that the file has to be embedded / attached to the email, I cannot add it as a URI unfortunately.

Can anyone help?

Update: Following arnaudq's advice, I have implemented wrapping the lines at 75 characters as mentioned in the RFC. The resulting MIME message looks like the following:

BEGIN:VCALENDAR
PRODID:-//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IwMTIgMTg6MzANCjkxNTY0MC8xCTI3LzExLzIwMTIgMTg6MzANCjkxNTY0MS8xCTI3L
 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

Unfortunately, this still doesn't work and the file (in this case a simple plain text file) does not come through with the calendar entry in Outlook.

What's really interesting is that saving the above MIME message to a file manually and renaming to a .ics then opening it does display the attached file correctly. This makes me think that there is something wrong with the way I'm sending the message, instead of the iCalendar markup.

Any ideas what is wrong?


回答1:


@paul, I made the following changes and its working fine for me. I need to verify this fix on all email clients. I tested on MS Outlook 2013, ios, MS Outlook 2010 and its working fine.

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

I am not modifying .ics file to add ATTACH property(ATTACH;ENCODING=BASE64;VALUE=BINARY;X-FILENAME=)




回答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);



回答3:


First, while you do make use of line breaks, it looks like you are not using the kind of line breaks that iCalendar expects. In short, each line after the first one should be prefixed with a space character and the lines should be less than 75 octets in length. See https://tools.ietf.org/html/rfc5545#section-3.1

(In general, for this type of interop issue, showing us the end result MIME message is more useful than the code that was used to generate it)

Then, as far as I remember, Outlook prefers attachments to be transmitted in a multipart/related containing the iCalendar stream and the attachment in different mime parts. See https://tools.ietf.org/html/rfc6047#section-4.6 for an example.

Finally, you might want to try sending an invitation with attachment from Outlook and see how the MIME message that it does send is structured.



来源:https://stackoverflow.com/questions/29104715/attaching-a-file-to-an-icalendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!