您可以使用iCal Standard (RFC 5545)通过邮件将会议请求发送到 Outlook
您不能以这种方式发送待办事项。
您可以发送“约会”,但这些在 Outlook 中显示为 .ics 附件,必须“盲目”接受。
会议请求显示在 Outlook 中,预览效果很好,可以接受或拒绝。
发送程序可以在发送后修改或取消会议。
使用DDay.iCal .Net Library 创建一个有效的 iCal 项目很容易
下面的代码是一个完整的工作示例。它使用有效的 iCal 会议请求构建字符串并通过邮件发送。
代码创建邮件:
- 简单邮件客户端的纯文本正文
- 用于在现代邮件客户端中显示的 HTML 正文
- iCal 会议请求为 AlternateView(将显示在 Outlook 中)
- iCal 会议请求作为附件(可用于 Outlook 以外的邮件客户端)
代码显示如何添加:
- HTML 形式的描述文本,在 Outlook 中看起来更好
- 优先级、可见性(公共/私人/机密)
- 可选的组织者(将显示在 Outlook 而不是邮件发件人中)
- 可选与会者
- 可选报警
- 会议的可选附件。将显示在 Outlook 的日历中
一些重要的细节:
- 邮件发件人(或可选的组织者)和邮件收件人必须不同才能在 Outlook 中进行这项工作
- .ics 中的 METHOD 和 Mime.ContentType 中的 METHOD 必须匹配
- 会议必须在未来进行才能使这项工作在 Outlook 中发挥作用
- .ics 部分必须是 MIME 邮件中的最后一个 alternateView 部分
有关 Outlook 解释 .ics 文件方式的确切细节在[MS-OXCICAL]: iCalendar to Appointment Object Conversion Algorithm中有详细说明
我们将使用这些程序集:
using System;
using System.IO;
using System.Net.Mail;
using DDay.iCal;
using DDay.iCal.Serialization.iCalendar;
对于 DDay.iCal,下载 DDay.iCal binary Files 就足够了。如果您想添加一些功能,最好查看 DDay.iCal 源代码,因为文档已过时,并且源代码包含相当完整的测试,可以使用其所有功能。
const string filepath = @"C:\temp\ical.test.ics";
// use PUBLISH for appointments
// use REQUEST for meeting requests
const string METHOD = "REQUEST";
// Properties of the meeting request
// keep guid in sending program to modify or cancel the request later
Guid uid = Guid.Parse("2B127C67-73B3-43C5-A804-5666C2CA23C9");
string VisBetreff = "This is the subject of the meeting request";
string TerminVerantwortlicherEmail = "mr.asker@myorg.com";
string bodyPlainText = "This is the simple iCal plain text msg";
string bodyHtml = "This is the simple <b>iCal HTML message</b>";
string location = "Meeting room 101";
// 1: High
// 5: Normal
// 9: low
int priority = 1;
//=====================================
MailMessage message = new MailMessage();
message.From = new MailAddress("sender@myorg.com");
message.To.Add(new MailAddress(TerminVerantwortlicherEmail));
message.Subject = "[VIS-Termin] " + VisBetreff;
// Plain Text Version
message.Body = bodyPlainText;
// HTML Version
string htmlBody = bodyHtml;
AlternateView HTMLV = AlternateView.CreateAlternateViewFromString(htmlBody,
new System.Net.Mime.ContentType("text/html"));
// iCal
IICalendar iCal = new iCalendar();
iCal.Method = METHOD;
iCal.ProductID = "My Metting Product";
// Create an event and attach it to the iCalendar.
Event evt = iCal.Create<Event>();
evt.UID = uid.ToString();
evt.Class = "PUBLIC";
// Needed by Outlook
evt.Created = new iCalDateTime(DateTime.Now);
evt.DTStamp = new iCalDateTime(DateTime.Now);
evt.Transparency = TransparencyType.Transparent;
// Set the event start / end times
evt.Start = new iCalDateTime(2014, 10, 3, 8, 0, 0);
evt.End = new iCalDateTime(2014, 10, 3, 8, 15, 0);
evt.Location = location;
//var organizer = new Organizer("the.organizer@myCompany.com");
//evt.Organizer = organizer;
// Set the longer description of the event, plain text
evt.Description = bodyPlainText;
// Event description HTML text
// X-ALT-DESC;FMTTYPE=text/html
var prop = new CalendarProperty("X-ALT-DESC");
prop.AddParameter("FMTTYPE", "text/html");
prop.AddValue(bodyHtml);
evt.AddProperty(prop);
// Set the one-line summary of the event
evt.Summary = VisBetreff;
evt.Priority = priority;
//--- attendes are optional
IAttendee at = new Attendee("mailto:Peter.Black@MyOrg.com");
at.ParticipationStatus = "NEEDS-ACTION";
at.RSVP = true;
at.Role = "REQ-PARTICIPANT";
evt.Attendees.Add(at);
// Let’s also add an alarm on this event so we can be reminded of it later.
Alarm alarm = new Alarm();
// Display the alarm somewhere on the screen.
alarm.Action = AlarmAction.Display;
// This is the text that will be displayed for the alarm.
alarm.Summary = "Upcoming meeting: " + VisBetreff;
// The alarm is set to occur 30 minutes before the event
alarm.Trigger = new Trigger(TimeSpan.FromMinutes(-30));
//--- Attachments
string filename = "Test.docx";
// Add an attachment to this event
IAttachment attachment = new DDay.iCal.Attachment();
attachment.Data = ReadBinary(@"C:\temp\Test.docx");
attachment.Parameters.Add("X-FILENAME", filename);
evt.Attachments.Add(attachment);
iCalendarSerializer serializer = new iCalendarSerializer();
serializer.Serialize(iCal, filepath);
// the .ics File as a string
string iCalStr = serializer.SerializeToString(iCal);
// .ics as AlternateView (used by Outlook)
// text/calendar part: method=REQUEST
System.Net.Mime.ContentType calendarType =
new System.Net.Mime.ContentType("text/calendar");
calendarType.Parameters.Add("method", METHOD);
AlternateView ICSview =
AlternateView.CreateAlternateViewFromString(iCalStr, calendarType);
// Compose
message.AlternateViews.Add(HTMLV);
message.AlternateViews.Add(ICSview); // must be the last part
// .ics as Attachment (used by mail clients other than Outlook)
Byte[] bytes = System.Text.Encoding.ASCII.GetBytes(iCalStr);
var ms = new System.IO.MemoryStream(bytes);
var a = new System.Net.Mail.Attachment(ms,
"VIS-Termin.ics", "text/calendar");
message.Attachments.Add(a);
// Send Mail
SmtpClient client = new SmtpClient();
client.Send(message);
这里是 ReadBinary() 函数:
private static byte[] ReadBinary(string fileName)
{
byte[] binaryData = null;
using (FileStream reader = new FileStream(fileName,
FileMode.Open, FileAccess.Read))
{
binaryData = new byte[reader.Length];
reader.Read(binaryData, 0, (int)reader.Length);
}
return binaryData;
}
在配置文件中配置 SmtpClient 最简单:
<configuration>
...
<system.net>
<mailSettings>
<smtp>
<network host="mysmtp.server.com" port="25" userName="mySmtpUserName" password="myPassword" />
</smtp>
</mailSettings>
</system.net>
...