کار با موارد تقویم Outlook

متدهای کمکی استفاده‌شده در این مقاله

برخی نمونه‌های زیر تاریخ‌های خود را با کمک‌کننده‌های کوچکی که در اینجا نشان داده شده‌اند می‌سازند. این‌ها بخشی از API Aspose.Email نیستند؛ آن‌ها را به کلاسی که کد شما در آن قرار دارد اضافه کنید.

// Months are 1-based here (7 = July), unlike java.util.Calendar
private static Date getDate(int year, int month, int day) {
    return getDate(year, month, day, 0, 0, 0);
}

private static Date getDate(int year, int month, int day, int hour, int minute, int second) {
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    calendar.clear();
    calendar.set(year, month - 1, day, hour, minute, second);
    return calendar.getTime();
}

private static Date addHours(Date date, int hours) {
    return new Date(date.getTime() + hours * 60L * 60 * 1000);
}

private static Date addDays(Date date, int days) {
    return addHours(date, days * 24);
}

// Counts the occurrences of an RRULE between two dates
private static long getOccurrenceCount(Date start, Date endBy, String rrule) {
    DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    CalendarRecurrence pattern = new CalendarRecurrence(
            "DTSTART:" + formatter.format(start) + "\r\nRRULE:" + rrule);
    DateCollection dates = pattern.generateOccurrences(start, endBy);
    return dates.size();
}

کار با MapiCalendar

Aspose.Email MapiCalendar کلاس متدها و ویژگی‌هایی را برای تنظیم ویژگی‌های مختلف یک مورد تقویم فراهم می‌کند. این مقاله نمونه کدهایی را برای موارد زیر ارائه می‌دهد:

ایجاد و ذخیرهٔ موارد تقویم

کد نمونهٔ زیر نشان می‌دهد چگونه می‌توانید یک مورد تقویم را در قالب ICS ایجاد و ذخیره کنید.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

Calendar cal = Calendar.getInstance();
cal.set(2012, Calendar.OCTOBER, 2, 13, 0, 0);
Date startDate = cal.getTime();
cal.set(2012, Calendar.OCTOBER, 2, 14, 0, 0);
Date endDate = cal.getTime();

MapiCalendar calendar = new MapiCalendar("LAKE ARGYLE WA 6743", 
                                         "Appointment", 
                                         "This is a very important meeting :)", 
                                         startDate, 
                                         endDate);

calendar.save(dataDir + "CalendarItem_out.ics", AppointmentSaveFormat.Ics);

ذخیرهٔ مورد تقویم به صورت MSG

کد نمونهٔ زیر نشان می‌دهد چگونه می‌توانید مورد تقویم را به صورت MSG ذخیره کنید.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
calendar.save(dataDir + "CalendarItemAsMSG_out.Msg", AppointmentSaveFormat.Msg);

تنظیم شناسه محصول هنگام ذخیرهٔ یک مورد تقویم به فرمت ICS

این ProductIdentifier ویژگی MapiCalendarIcsSaveOptions کلاس برای ذخیره یک مورد تقویم MAPI به فایل iCalendar (ICS) استفاده می‌شود به‌طوری که اطلاعات تاریخ و زمان اصلی و همچنین شناسهٔ محصول سفارشی حفظ شود. این ویژگی شناسهٔ محصولی که شیء iCalendar را ایجاد کرده است را مشخص می‌کند.

نمونه کد زیر نشان می‌دهد چگونه با داده‌های iCalendar (ICS) درون یک شیء تقویم MAPI کار کنید:

MapiCalendarIcsSaveOptions icsSaveOptions = new MapiCalendarIcsSaveOptions();
icsSaveOptions.setKeepOriginalDateTimeStamp(true);
icsSaveOptions.setProductIdentifier("Foo Ltd");

mapiCalendar.save("my.ics", icsSaveOptions);

خواندن و تنظیم شناسه محصول یک آیتم تقویم

یک فایل iCalendar محصولی که آن را ایجاد کرده در PRODID فیلد. Aspose.Email این شناسه را هنگام تبدیل یک آیتم تقویم بین ICS و MSG حفظ می‌کند، بنابراین می‌توانید منبع آیتم را ردیابی کنید. شناسه از طریق اعضای زیر در دسترس است:

  • Appointment.getProductId() و setProductId(String) - دریافت یا تنظیم شناسه محصولی که یک Appointment.
  • MapiCalendar.getProductId() و setProductId(String) - دریافت یا تنظیم شناسه محصولی که یک MapiCalendar.
  • MapiCalendarSaveOptions.setProductIdentifier(String) - شناسه‌ای که هنگام ذخیره تقویم با MapiCalendarMsgSaveOptions یا MapiCalendarIcsSaveOptions.

قطعه کد زیر شناسه محصول را از یک فایل ICS می‌خواند، قرار ملاقات را به MSG تبدیل می‌کند و با شناسه جدید ذخیره می‌کند:

// Read the product identifier from an iCalendar file
Appointment appointment = Appointment.load("meeting.ics");
System.out.println("Appointment product ID: " + appointment.getProductId());

// The identifier is kept when the appointment is converted to MSG
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
appointment.save(msgStream, new AppointmentMsgSaveOptions());
MapiMessage msg = MapiMessage.load(new ByteArrayInputStream(msgStream.toByteArray()));
MapiCalendar calendar = (MapiCalendar) msg.toMapiMessageItem();
System.out.println("MapiCalendar product ID: " + calendar.getProductId());

// Save the calendar item to MSG with a different product identifier
MapiCalendarMsgSaveOptions options = new MapiCalendarMsgSaveOptions();
options.setProductIdentifier("-//Contoso//Calendar 2.0//EN");
calendar.save("meeting_out.msg", options);

شناسه‌ای که با setProductId در PRODID فیلد هنگام ذخیره آیتم به ICS:

calendar.setProductId("-//Contoso//Calendar 2.0//EN");
calendar.save("meeting_out.ics", new MapiCalendarIcsSaveOptions());

افزودن یادآوری نمایش به تقویم

کد نمونهٔ زیر نشان می‌دهد چگونه می‌توانید یک یادآوری نمایش به تقویم اضافه کنید.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
Calendar jCalendar = Calendar.getInstance();
jCalendar.add(Calendar.HOUR, 1);
Date startDate = jCalendar.getTime();
Date endDate = jCalendar.getTime();

MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("attendee@domain.com", "Attendee"));
// Create Appointment
Appointment app = new Appointment("Home", 
                                  startDate, 
                                  endDate, 
                                  new MailAddress("organizer@domain.com", "Organizer"), 
                                  attendees);
MailMessage msg = new MailMessage();
msg.addAlternateView(app.requestApointment());
MapiMessage mapi = MapiMessage.fromMailMessage(msg);
MapiCalendar calendar = (MapiCalendar) mapi.toMapiMessageItem();

// Set calendar Properties
calendar.setReminderSet(true);
calendar.setReminderDelta(5); // 45 min before start of event

String savedFile = (dataDir + "calendarWithDisplayReminder.ics");
calendar.save(savedFile, AppointmentSaveFormat.Ics);

افزودن یادآوری صوتی به تقویم

کد نمونهٔ زیر نشان می‌دهد چگونه می‌توانید یک یادآوری صوتی به تقویم اضافه کنید.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
Calendar jCalendar = Calendar.getInstance();
jCalendar.add(Calendar.HOUR, 1);
Date startDate = jCalendar.getTime();
Date endDate = jCalendar.getTime();

MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("attendee@domain.com", "Attendee"));

Appointment app = new Appointment("Home", 
                                  startDate, 
                                  endDate, 
                                  new MailAddress("organizer@domain.com", "Organizer"), 
                                  attendees);

MailMessage msg = new MailMessage();
msg.addAlternateView(app.requestApointment());
MapiMessage mapi = MapiMessage.fromMailMessage(msg);
MapiCalendar cal = (MapiCalendar) mapi.toMapiMessageItem();

cal.setReminderSet(true);
cal.setReminderDelta(58); // 58 min before start of event
cal.setReminderFileParameter(dataDir + "Alarm01.wav");

String savedFile = dataDir + "calendarWithAudioReminder_out.ics";
cal.save(savedFile, AppointmentSaveFormat.Ics);

افزودن/بازخوانی پیوست‌ها از فایل‌های تقویم

کد نمونهٔ زیر نشان می‌دهد چگونه می‌توانید پیوست‌ها را به فایل‌های تقویم اضافه/بازخوانی کنید.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
Calendar jCalendar = Calendar.getInstance();
jCalendar.add(Calendar.HOUR, 1);
Date startDate = jCalendar.getTime();
Date endDate = jCalendar.getTime();

String[] files = new String[3];
files[0] = "attachment_1.doc";
files[1] = "download.png";
files[2] = "Desert.jpg";

MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("attendee@domain.com", "Attendee"));

Appointment app1 = new Appointment("Home", 
                                   startDate, 
                                   endDate, 
                                   new MailAddress("organizer@domain.com", "Organizer"), 
                                   attendees);
for (String file : files) {
    app1.getAttachments().addItem(new Attachment(dataDir + file));
}

app1.save(dataDir + "appWithAttachments_out.ics", AppointmentSaveFormat.Ics);

Appointment app2 = Appointment.load(dataDir + "appWithAttachments_out.ics");
System.out.println(app2.getAttachments().size());
for (Attachment att : app2.getAttachments())
    System.out.println(att.getName());

وضعیت دریافت‌کنندگان از یک درخواست جلسه

کد نمونهٔ زیر نشان می‌دهد چگونه می‌توانید وضعیت دریافت‌کنندگان را از یک درخواست جلسه دریافت کنید.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String fileName = "outlook/test.msg";

MapiMessage message = MapiMessage.load(fileName);
for (MapiRecipient recipient : message.getRecipients()) {
    System.out.println(recipient.getRecipientTrackStatus());
}

تنظیم صریح وضعیت قرار ملاقات تقویم

Aspose.Email به شما امکان می‌دهد وضعیت یک MapiCalendar شیء به‌وضوح، که رفتار پیش‌فرض را نادیده می‌گیرد. این setStateForced روش کنترل بهتری بر وضعیت رویدادهای تقویم فراهم می‌کند، به‌ویژه هنگام پردازش درخواست‌های جلسه دریافت‌شده. به‌صورت پیش‌فرض، وقتی یک جلسه ایجاد می‌شود، وضعیت آن MapiCalendarState.Meeting. وقتی در صندوق ورودی گیرنده دریافت می‌شود، به‌صورت خودکار تغییر می‌یابد به MapiCalendarState.Received، و کلاس پیام آن به‌روزرسانی می‌شود به IPM.Schedule.Meeting.Request. با استفاده از SetStateForced اجازه می‌دهد حالت را به صورت دستی به Received تنظیم کنید، که می‌تواند برای حفظ اطلاعات برگزارکننده هنگام ذخیره تقویم به عنوان فایل MSG مفید باشد. با این حال، ممکن است ارسال مجدد یا فوروارد صحیح جلسه را مانع شود.

نمونه کد زیر به شما کمک می‌کند این ویژگی را پیاده‌سازی کنید:

Calendar c = Calendar.getInstance();
c.set(2024, Calendar.MAY, 10, 12, 30, 0);
Date startDate = c.getTime();
c.set(2024, Calendar.MAY, 10, 13, 30, 0);
Date endDate = c.getTime();
MapiCalendar appointment = new MapiCalendar(
        "LAKE ARGYLE WA 6743",
        "Appointment",
        "This is a very important meeting :)",
        startDate,
        endDate);

MapiElectronicAddress organizer = new MapiElectronicAddress();
organizer.setEmailAddress("test@aaa.com");
organizer.setDisplayName("test display Name");
appointment.setOrganizer(organizer);

appointment.setStateForced(MapiCalendarState.Meeting | MapiCalendarState.Received);

appointment.save("appointment.msg", AppointmentSaveFormat.Msg);

ایجاد MapiCalendarTimeZone از منطقه زمانی استاندارد

قطعه کد زیر نشان می‌دهد چگونه یک MapiCalendarTimeZone از منطقه زمان استاندارد.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone("Eastern Standard Time");

تنظیم یادآوری با قرار ملاقات ایجاد شده

یک یادآوری می‌تواند هنگام ایجاد یک قرار ملاقات اضافه شود. این هشدارها می‌توانند بر اساس معیارهای مختلف مانند n دقیقه قبل از شروع برنامه، تکرار n بار با فواصل n فعال شوند. می‌توان از برچسب‌های متفاوت برای ایجاد این فعال‌کننده‌ها در اسکریپتی که بین BEGIN:VALARM و END:VALARM در یک قرار ملاقات قرار می‌گیرد، استفاده کرد. انواع مختلفی برای تنظیم یادآوری روی یک قرار ملاقات وجود دارد.

تنظیم یک یادآوری با افزودن برچسب‌ها

کد نمونهٔ زیر نشان می‌دهد چگونه می‌توانید یادآوری را با افزودن برچسب‌ها تنظیم کنید.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";
long MIN_MS = 60 * 1000;
long HR_MS = 60 * MIN_MS;
long DAY_MS = 24 * HR_MS;

String location = "Meeting Location: Room 5";
Date startDate = getDate(1997, 3, 18, 18, 30, 00);
Date endDate = getDate(1997, 3, 18, 19, 30, 00);
MailAddress organizer = new MailAddress("aaa@amail.com", "Organizer");
MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("bbb@bmail.com", "First attendee"));

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

// Audio alarm that will sound at a precise time and
// Repeat 4 more times at 15 minute intervals:
AppointmentReminder audioReminder = new AppointmentReminder();
audioReminder.setTrigger(new ReminderTrigger(getDate(1997, 3, 17, 13, 30, 0)));
audioReminder.setRepeat(4);
audioReminder.setDuration(new ReminderDuration(15 * MIN_MS));
audioReminder.setAction(ReminderAction.Audio);
ReminderAttachment attach = new ReminderAttachment(new URI("ftp://Host.com/pub/sounds/bell-01.aud"));
audioReminder.getAttachments().addItem(attach);
target.getReminders().addItem(audioReminder);


// Display alarm that will trigger 30 minutes before the
// Scheduled start of the event it is
// Associated with and will repeat 2 more times at 15 minute intervals:
AppointmentReminder displayReminder = new AppointmentReminder();
ReminderDuration dur = new ReminderDuration(-30 * MIN_MS);
displayReminder.setTrigger(new ReminderTrigger(dur, ReminderRelated.Start));
displayReminder.setRepeat(2);
displayReminder.setDuration(new ReminderDuration(15 * MIN_MS));
displayReminder.setAction(ReminderAction.Display);
displayReminder.setDescription("Breakfast meeting with executive team at 8:30 AM EST");
target.getReminders().addItem(displayReminder);

// Email alarm that will trigger 2 days before the
// Scheduled due date/time. It does not
// Repeat. The email has a subject, body and attachment link.
AppointmentReminder emailReminder = new AppointmentReminder();
ReminderDuration dur1 = new ReminderDuration(-2 * DAY_MS);
emailReminder.setTrigger(new ReminderTrigger(dur1, ReminderRelated.Start));
ReminderAttendee attendee = new ReminderAttendee("john_doe@host.com");
emailReminder.getAttendees().addItem(attendee);
emailReminder.setAction(ReminderAction.Email);
emailReminder.setSummary("REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING");
emailReminder.setDescription("A draft agenda needs to be sent out to the attendees to the weekly managers meeting (MGR-LIST). Attached is a pointer the document template for the agenda file.");
ReminderAttachment attach1 = new ReminderAttachment(new URI("http://Host.com/templates/agenda.doc"));
emailReminder.getAttachments().addItem(attach1);
target.getReminders().addItem(emailReminder);

// Procedural alarm that will trigger at a precise date/time
// And will repeat 23 more times at one hour intervals. The alarm will
// Invoke a procedure file.
AppointmentReminder procReminder = new AppointmentReminder();
procReminder.setTrigger(new ReminderTrigger(getDate(1998, 1, 1, 5, 0, 0)));
procReminder.setRepeat(23);
procReminder.setDuration(new ReminderDuration(1 * DAY_MS));
procReminder.setAction(ReminderAction.Procedure);
ReminderAttachment attach2 = new ReminderAttachment(new URI("ftp://Host.com/novo-procs/felizano.exe"));
procReminder.getAttachments().addItem(attach2);
target.getReminders().addItem(procReminder);
target.save(dataDir + "savedFile_out.ics");

تبدیل Appointment EML به MSG با بدنهٔ HTML

از نسخهٔ 19.3 به بعد، Aspose.Email امکان تبدیل Appointment EML به MSG را فراهم می‌کند در حالی که بدنهٔ HTML قرار ملاقات حفظ می‌شود. Aspose.Email یک MapiConversionOptions.ForcedRtfBodyForAppointment خاصیتی که مقدار پیش‌فرض آن true. است. وقتی مقدار MapiConversionOptions.ForcedRtfBodyForAppointment اگر به true تنظیم شود، بدنهٔ قرار ملاقات به قالب RTF تبدیل می‌شود. برای حفظ قالب بدنهٔ قرار ملاقات به فرمت HTML، مقدار MapiConversionOptions.ForcedRtfBodyForAppointment به false.

مثال زیر نحوه استفاده از MapiConversionOptions.ForcedRtfBodyForAppointment خاصیتی برای حفظ قالب بدنه قرار ملاقات به فرمت HTML.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

MailMessage mailMessage = MailMessage.load(dataDir + "TestAppointment.eml");

MapiConversionOptions conversionOptions = new MapiConversionOptions();
conversionOptions.setFormat(OutlookMessageFormat.Unicode);

// default value for ForcedRtfBodyForAppointment is true
conversionOptions.setForcedRtfBodyForAppointment(false);

MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage, conversionOptions);

if (mapiMessage.getBodyType() == BodyContentType.Html) {
    System.out.println("Body Type: Html");
} else if (mapiMessage.getBodyType() == BodyContentType.Rtf) {
    System.out.println("Body Type: Rtf");
}

mapiMessage.save(dataDir + "TestAppointment_out.msg");