مدیریت ملاقات‌ها: ایجاد و دستکاری، تبدیل ICS به MSG

ایجاد یک ملاقات و ذخیره به دیسک با قالب MSG یا ICS

این Appointment کلاس در Aspose.Email برای .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");

سفارشی‌سازی ملاقات‌ها

تنظیم وضعیت شرکت‌کنندگان برای شرکت‌کنندگان قرار ملاقات

API Aspose.Email برای .NET به شما اجازه می‌دهد وضعیت شرکت‌کنندگان ملاقات را هنگام تنظیم پیام پاسخ تنظیم کنید. این کار ویژگی 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

API Aspose.Email برای .NET امکان دریافت یا تنظیم شناسه محصولی که شیء 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
}