gpt4 book ai didi

c# - 将文件附加到 iCalendar

转载 作者:行者123 更新时间:2023-12-01 22:41:23 29 4
gpt4 key购买 nike

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

这是我正在使用的代码:

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new System.Net.Mail.MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e591809691d4a591809691cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>", "test1");
msg.To.Add(new System.Net.Mail.MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d19081e195f2d19081e19430e0200" rel="noreferrer noopener nofollow">[email protected]</a>", "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 ( https://www.rfc-editor.org/rfc/rfc5545 ),我认为我已经按照规范所述完成了所有操作。我猜测文件读取方式(Convert.ToBase64String 位)存在问题,或者我缺少备用 View 的某些内容(我见过其他人添加多个 View )。

我尝试过的事情:

  • 替换 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:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="24504157501564504157500a474b49" rel="noreferrer noopener nofollow">[email protected]</a>
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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84f0e1f7f0b6c4f0e1f7f0aae7ebe9" rel="noreferrer noopener nofollow">[email protected]</a>":mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3b7a6b0b7f183b7a6b0b7eda0acae" rel="noreferrer noopener nofollow">[email protected]</a>
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 标记有问题。

有什么问题吗?

最佳答案

@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=)

关于c# - 将文件附加到 iCalendar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104715/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com