จัดการนัดหมาย: สร้างและแก้ไข, แปลงไฟล์ ICS เป็น MSG

สร้างการนัดหมายและบันทึกลงดิสก์ในรูปแบบ MSG หรือ ICS

นี้ Appointment คลาสใน Aspose.Email for .NET สามารถใช้สร้างการนัดหมายใหม่ได้ ในบทความนี้ เราจะสร้างการนัดหมายและบันทึกลงดิสก์ในรูปแบบ ICS ขั้นตอนต่อไปนี้จำเป็นสำหรับการสร้างการนัดหมายและบันทึกลงดิสก์.

  1. สร้างอินสแตนซ์ของ Appointment คลาสและเริ่มต้นด้วยคอนสตรกเตอร์นี้.
  2. ส่งอาร์กิวเมนต์ต่อไปนี้ในคอนสตรกเตอร์ด้านบน
    1. สถานที่
    2. สรุป
    3. คำอธิบาย
    4. วันที่เริ่มต้น
    5. วันที่สิ้นสุด
    6. ผู้จัด
    7. ผู้เข้าร่วม
  3. เรียกใช้ Save() เมธอดและระบุชื่อไฟล์และรูปแบบในอาร์กิวเมนต์.

การนัดหมายสามารถเปิดได้ใน Microsoft Outlook หรือโปรแกรมใดก็ได้ที่สามารถโหลดไฟล์ ICS หากไฟล์เปิดใน Microsoft Outlook จะเพิ่มการนัดหมายลงในปฏิทิน Outlook โดยอัตโนมัติ.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีสร้างและบันทึกการนัดหมายลงดิสก์ในรูปแบบ ICS หรือ MSG.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET

// Create and initialize an instance of the Appointment class
Appointment appointment = new Appointment(
    "Meeting Room 3 at Office Headquarters",// Location
    "Monthly Meeting",                      // Summary
    "Please confirm your availability.",    // Description
    new DateTime(2015, 2, 8, 13, 0, 0),     // Start date
    new DateTime(2015, 2, 8, 14, 0, 0),     // End date
    "from@domain.com",                      // Organizer
    "attendees@domain.com");                // Attendees

// Save the appointment to disk in ICS format            
appointment.Save(fileName + ".ics", new AppointmentIcsSaveOptions());
Console.WriteLine("Appointment created and saved to disk successfully.");

// Save the appointment to disk in MSG format
appointment.Save(fileName + ".msg", new AppointmentMsgSaveOptions(););
Console.WriteLine("Appointment created and saved to disk successfully.");

สร้างการนัดหมายด้วยเนื้อหา HTML

คุณสามารถระบุการแสดงผลแบบอื่นของคำอธิบายเหตุการณ์ในประเภทเนื้อหาต่างๆ โดยใช้หัวข้อ X-ALT-DESC ซึ่งทำให้ผู้รับไฟล์ iCalendar เลือกการแสดงผลที่เหมาะสมที่สุด ตัวอย่างเช่น คุณอาจใส่คำอธิบายแบบข้อความธรรมดาโดยใช้ประเภทเนื้อหา "text/plain" และคำอธิบายแบบ HTML โดยใช้ประเภทเนื้อหา "text/html" หัวข้อ X-ALT-DESC จะถูกเพิ่มสำหรับแต่ละการแสดงผลทางเลือก เพื่อสร้างการนัดหมายด้วยเนื้อหา HTML ให้ตั้งค่า HtmlDescription คุณสมบัติ.

ลองใช้ตัวอย่างโค้ดต่อไปนี้เพื่อสร้างการนัดหมายพร้อมคำอธิบาย HTML ทางเลือก:

  1. สร้างอินสแตนซ์ใหม่ของคลาส Appointment.
  2. จัดเตรียมพารามิเตอร์ที่จำเป็นให้กับคอนสตรัคเตอร์ Appointment:
    • ระบุตำแหน่งของการนัดหมาย.
    • ตั้งค่าวันและเวลาเริ่มต้น.
    • ตั้งค่าวันและเวลาสิ้นสุด.
    • ระบุผู้จัดการประชุม.
    • ระบุผู้เข้าร่วม.
  3. ตั้งค่า HtmlDescription คุณสมบัติของอ็อบเจ็กต์การนัดหมาย แสดงว่าคำอธิบายอยู่ในรูปแบบ HTML
  4. ตั้งค่าคุณสมบัติ Description ของอ็อบเจ็กต์การนัดหมายเป็นสตริงรูปแบบ HTML ที่อยู่ระหว่างสตริงหลายบรรทัด:
    • มาร์กอัป HTML มีส่วน <style> ที่กำหนดคลาส CSS ชื่อ "text" พร้อมสไตล์ฟอนต์
    • ส่วนเนื้อหา HTML มีแท็กย่อหน้า <p> พร้อมคลาส CSS "text" และข้อความเชิญจริง
  5. อ็อบเจ็กต์การนัดหมายพร้อมใช้งานแล้ว คุณสามารถดำเนินการต่อหรือบันทึกเป็นไฟล์ iCalendar
var appointment = new Appointment("Bygget 83",
    DateTime.UtcNow, // start date
    DateTime.UtcNow.AddHours(1), // end date
    new MailAddress("TintinStrom@from.com", "Tintin Strom"), // organizer
    new MailAddress("AinaMartensson@to.com", "Aina Martensson")) // attendee
{
    HtmlDescription = @"
    <html>
     <style type=""text/css"">
      .text {
             font-family:'Comic Sans MS';
             font-size:16px;
            }
     </style>
    <body>
     <p class=""text"">Hi, I'm happy to invite you to our party.</p>
    </body>
    </html>"
};

สร้างคำขอการนัดหมายแบบร่าง

ในบทความก่อนหน้าได้แสดงวิธีสร้างและบันทึกการนัดหมายในรูปแบบ ICS ซึ่งมักต้องการสร้างคำขอการนัดหมายในโหมดร่าง เพื่อให้ข้อมูลพื้นฐานถูกเพิ่มและจากนั้นการนัดหมายร่างเดียวกันสามารถส่งต่อไปยังผู้ใช้รายอื่นเพื่อทำการปรับเปลี่ยนตามการใช้งานของแต่ละบุคคล เพื่อบันทึกการนัดหมายในโหมดร่าง, MethodType คุณสมบัติของคลาส Appointment ควรตั้งค่าเป็น AppointmentMethodType.Publish. โค้ดตัวอย่างต่อไปนี้แสดงวิธีสร้างคำขอนัดหมายแบบร่าง

string sender = "test@gmail.com";
string recipient = "test@email.com";

MailMessage message = new MailMessage(sender, recipient, string.Empty, string.Empty);

Appointment app = new Appointment(string.Empty, DateTime.Now, DateTime.Now, sender, recipient)
{
    MethodType = AppointmentMethodType.Publish
};

message.AddAlternateView(app.RequestApointment());

MapiMessage msg = MapiMessage.FromMailMessage(message);

// Save the appointment as draft.
msg.Save(dstDraft);

Console.WriteLine(Environment.NewLine + "Draft saved at " + dstDraft);

สร้างการนัดหมายแบบร่างจากข้อความ

โค้ดตัวอย่างต่อไปนี้แสดงวิธีสร้างร่างนัดหมายจากข้อความ. 

string ical = @"BEGIN:VCALENDAR
METHOD:PUBLISH
PRODID:-//Aspose Ltd//iCalender Builder (v3.0)//EN
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;CN=test@gmail.com:mailto:test@gmail.com
DTSTART:20130220T171439
DTEND:20130220T174439
DTSTAMP:20130220T161439Z
END:VEVENT
END:VCALENDAR";

string sender = "test@gmail.com";
string recipient = "test@email.com";
MailMessage message = new MailMessage(sender, recipient, string.Empty, string.Empty);
AlternateView av = AlternateView.CreateAlternateViewFromString(ical, new ContentType("text/calendar"));
message.AlternateViews.Add(av);
MapiMessage msg = MapiMessage.FromMailMessage(message);
msg.Save(dataDir + "draft_out.msg");

ปรับแต่งการนัดหมาย

ตั้งค่าสถานะผู้เข้าร่วมของการนัดหมาย

Aspose.Email for .NET API ให้คุณตั้งค่าสถานะของผู้เข้าร่วมการนัดหมายขณะจัดทำข้อความตอบกลับ ซึ่งจะเพิ่มคุณสมบัติ PARTSTAT ลงในไฟล์ ICS

DateTime startDate = new DateTime(2011, 12, 10, 10, 12, 11),
         endDate = new DateTime(2012, 11, 13, 13, 11, 12);
MailAddress organizer = new MailAddress("aaa@amail.com", "Organizer");
MailAddressCollection attendees = new MailAddressCollection();
MailAddress attendee1 = new MailAddress("bbb@bmail.com", "First attendee");
MailAddress attendee2 = new MailAddress("ccc@cmail.com", "Second attendee");

attendee1.ParticipationStatus = ParticipationStatus.Accepted;
attendee2.ParticipationStatus = ParticipationStatus.Declined;
attendees.Add(attendee1);
attendees.Add(attendee2);

Appointment target = new Appointment(location, startDate, endDate, organizer, attendees);

ปรับแต่งตัวระบุผลิตภัณฑ์สำหรับ iCalendar

Aspose.Email for .NET API ให้สามารถรับหรือกำหนดตัวระบุผลิตภัณฑ์ที่สร้างวัตถุ iCalendar

string description = "Test Description";
Appointment app = new Appointment("location", "test appointment", description, DateTime.Today,
DateTime.Today.AddDays(1), "first@test.com", "second@test.com");

IcsSaveOptions saveOptions = IcsSaveOptions.Default;
saveOptions.ProductId = "Test Corporation";
app.Save(dataDir + "ChangeProdIdOfICS.ics", saveOptions);

การโหลดการนัดหมาย

นอกจากนี้ Appointment คลาสนี้สามารถใช้โหลดการนัดหมายจากไฟล์ ICS

โหลดการนัดหมายในรูปแบบ ICS

เพื่อโหลดการนัดหมายในรูปแบบ ICS จำเป็นต้องทำขั้นตอนต่อไปนี้:

  1. สร้างอินสแตนซ์ของ Appointment คลาส.
  2. เรียกใช้ Load() เมธอดโดยการระบุพาธของไฟล์ ICS.
  3. อ่านคุณสมบัติใด ๆ เพื่อรับข้อมูลจากนัดหมาย (ไฟล์ ICS).

โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดการนัดหมายในรูปแบบ ICS.

// Load an Appointment just created and saved to disk and display its details.
Appointment loadedAppointment = Appointment.Load(dstEmail);
Console.WriteLine(Environment.NewLine + "Loaded Appointment details are as follows:");
// Display the appointment information on screen
Console.WriteLine("Summary: " + loadedAppointment.Summary);
Console.WriteLine("Location: " + loadedAppointment.Location);
Console.WriteLine("Description: " + loadedAppointment.Description);
Console.WriteLine("Start date: " + loadedAppointment.StartDate);
Console.WriteLine("End date: " + loadedAppointment.EndDate);
Console.WriteLine("Organizer: " + appointment.Organizer);
Console.WriteLine("Attendees: " + appointment.Attendees);
Console.WriteLine(Environment.NewLine + "Appointment loaded successfully from " + dstEmail);

แปลงไฟล์ ICS เป็น MSG

API ช่วยให้คุณแปลงการนัดหมายเป็นอ็อบเจ็กต์ข้อความได้อย่างง่ายดาย ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแปลงคำขอนัดหมายเป็น MailMessage หรือ MapiMessage:

var appointment = Appointment.Load("appRequest.ics");

var eml = appointment.ToMailMessage();
var msg = appointment.ToMapiMessage();

อ่านหลายเหตุการณ์จากไฟล์ ICS

List<Appointment> appointments = new List<Appointment>();
CalendarReader reader = new CalendarReader(dataDir + "US-Holidays.ics");

while (reader.NextEvent())
{
    appointments.Add(reader.Current);
}
//working with appointments...

เขียนหลายเหตุการณ์ลงไฟล์ ICS

IcsSaveOptions saveOptions = new IcsSaveOptions();
saveOptions.Action = AppointmentAction.Create;
using (CalendarWriter writer = new CalendarWriter(dataDir + "WriteMultipleEventsToICS_out.ics", saveOptions))
{
    for (int i = 0; i < 10; i++)
    {
        Appointment app = new Appointment(string.Empty, DateTime.Now, DateTime.Now, "sender@domain.com", "receiver@domain.com");
        app.Description = "Test body " + i;
        app.Summary = "Test summary:" + i;
        writer.Write(app);
    }
}

กำหนดเวอร์ชันของการนัดหมาย

เพื่อกำหนดเวอร์ชันของการนัดหมาย คุณสามารถใช้ Appointment.Version คุณสมบัติของ Appointment คลาสนี้ คุณสมบัตินี้ช่วยในการระบุว่าไฟล์ของพวกเขาพัฒนาบนเวอร์ชันใด เพื่อให้แน่ใจว่าเข้ากันได้กับระบบและแอปอื่นๆ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีนำคุณสมบัตินี้ไปใช้ในโครงการของคุณ:

var app = Appointment.Load("meeting.ics");

if (app.Version == 1.0)
{
    // do something
}