עבודה עם פגישות
טעינה ושמירה של פגישה בפורמט ICS
ה Appointment מחלקה ב‑Aspose.Email for Java יכולה לשמש לטעון אפונטמנט בפורמט ICS וכן ליצור אפונטמנט חדש ולשמור אותו לדיסק בפורמט זה. במאמר זה, תחילה ניצור אפונטמנט ונשמור אותו בפורמט ICS ולאחר מכן נטען אותו חזרה.
טעינת פגישה בפורמט ICS
כדי לטעון פגישה בפורמט ICS, יש לבצע את הצעדים הבאים:
- צור מופע של Appointment מחלקה.
- הפעל את load() שיטה, המספקת את נתיב קובץ ה‑ICS.
- קרא את המאפיינים שלו כדי לקבל מידע מהאפונטמנט (קובץ ICS).
קוד המקור הבא מציג כיצד לטעון אפונטמנט בפורמט ICS.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
// Load the appointment from an ICS file
Appointment appointment = Appointment.load(dataDir + "appointment.ics");
// Read its properties
System.out.println("Summary: " + appointment.getSummary());
System.out.println("Location: " + appointment.getLocation());
System.out.println("Description: " + appointment.getDescription());
System.out.println("Start date: " + appointment.getStartDate());
System.out.println("End date: " + appointment.getEndDate());
System.out.println("Organizer: " + appointment.getOrganizer());
System.out.println("Attendees: " + appointment.getAttendees());
יצירת פגישה ושמירה לדיסק בפורמט ICS
הצעדים הבאים נדרשים ליצירת פגישה ולשמירתה בפורמט ICS.
- צור מופע של Appointment מחלקה, שמעבירה מיקום, סיכום, תיאור, תאריך התחלה, תאריך סיום, מארגן ומשתתפים לבונה שלה.
- הגדר תאריכי יצירה ושינוי אם אתה צריך אותם בפלט.
- הפעל את save() שיטה, שמציינת את שם הקובץ והפורמט.
ניתן לפתוח את הפגישה ב‑Microsoft Outlook או בכל תוכנה שיכולה לטעון קובץ ICS. אם הקובץ נפתח ב‑Microsoft Outlook הוא מוסיף אוטומטית את הפגישה ללוח השנה של Outlook.
קוד המקור הבא מציג כיצד ליצור אפונטמנט ולשמור אותו לדיסק בפורמט ICS.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
// Set the start and end date of the appointment
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 10, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 11, 0, 0);
Date endDate = calendar.getTime();
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(new MailAddress("attendee@domain.com", "Attendee"));
// Location, summary, description, start, end, organizer and attendees
Appointment appointment = new Appointment(
"Room 112",
"Monthly Meeting",
"Please confirm your attendance.",
startDate,
endDate,
new MailAddress("organizer@domain.com", "Organizer"),
attendees);
appointment.setCreatedDate(new Date());
appointment.setLastModifiedDate(new Date());
// Save it in ICS format
appointment.save(dataDir + "appointment_out.ics", AppointmentSaveFormat.Ics);
שמירת פגישות לפורמט MSG
Aspose.Email מאפשר לשמור אפונטמנטים ישירות לקבצי .msg. המחלקות הציבוריות הבאות זמינות להתאמה של אופן שמירת האפונטמנטים:
- AppointmentMsgSaveOptions מחלקה עם אפשרויות נוספות לשמירת פגישות בפורמט msg.
- AppointmentIcsSaveOptions מחלקה עם אפשרויות נוספות כדי לשמור אפונטמנט בפורמט ICS. היא מחליפה את ה‑
IcsSaveOptions.
דוגמת הקוד למטה מראה כיצד לטעון פגישה מקובץ, ולאחר מכן לשמור אותה בשני פורמטים שונים: .ics ו‑.msg.
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "appointment.ics");
appointment.save(dataDir + "appointment_out.ics", new AppointmentIcsSaveOptions());
appointment.save(dataDir + "appointment_out.msg", new AppointmentMsgSaveOptions());
יצירת פגישה עם תוכן HTML
זה נהוג להשתמש בכותרת X-ALT-DESC בפורמט iCalendar (RFC 5545). זוהי מאפיין מורחב המספק תיאור חלופי קריא לבני אדם של פריט לוח שנה או אירוע. כותרת זו משמשת לעיתים לכלול ייצוג טקסט פשוט או HTML של תיאור האירוע, מה שיכול להיות שימושי להתאמה עם תוכנות לוח שנה ישנות או להציע גרסה מקוצרת של התיאור. במקרים שבהם התיאור הראשי אינו נתמך או מוצג כראוי על‑ידי אפליקציית הלוח של הנמען, נעשה שימוש בכותרת X-ALT-DESC כדי לספק תיאור חלופי של האירוע. היא מאפשרת לשולח לכלול ייצוגים שונים של תיאור האירוע כדי להבטיח תאימות ונגישות משופרת בין תוכנות ופלטפורמות לוח שנה שונות. כדי ליצור אפונטמנט עם תוכן HTML, העבר את המארקאפ ל‑ setHtmlDescription שיטה. קטע הקוד למטה יוצר אפונטמנט עם תאריך, זמן, מיקום, מארגן, משתתף ותיאור מפורמט:
Date startDate = new Date();
// One hour later
Date endDate = new Date(startDate.getTime() + 60 * 60 * 1000);
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(new MailAddress("AinaMartensson@to.com", "Aina Martensson"));
Appointment appointment = new Appointment("Bygget 83",
startDate, // start date
endDate, // end date
new MailAddress("TintinStrom@from.com", "Tintin Strom"), // organizer
attendees); // attendees
appointment.setHtmlDescription("<html>\n"
+ " <style type=\"text/css\">\n"
+ " .text {\n"
+ " font-family:'Comic Sans MS';\n"
+ " font-size:16px;\n"
+ " }\n"
+ " </style>\n"
+ " <body>\n"
+ " <p class=\"text\">Hi, I'm happy to invite you to our party.</p>\n"
+ " </body>\n"
+ " </html>");
יצירת בקשת פגישה בטיוטה
כדי לשמור אפונטמנט במצב טיוטה, סוג השיטה של ה‑ Appointment המחלקה צריכה להיות מוגדרת ל‑Publish עם setMethodType. שים לב לשם הגישה: ב‑Aspose.Email for Java זהו setMethodType, לא setMethod. קטע הקוד הבא מדגים את השימוש בו.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
String sender = "sender@domain.com";
String recipient = "recipient@domain.com";
MailMessage message = new MailMessage(sender, recipient, "", "");
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2026, java.util.Calendar.DECEMBER, 1, 0, 0, 0);
Date endDate = calendar.getTime();
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(new MailAddress("attendee@domain.com", "Attendee"));
Appointment app = new Appointment("", startDate, endDate,
new MailAddress("organizer@domain.com", "Organizer"), attendees);
// Publish marks the appointment as a draft rather than a request
app.setMethodType(AppointmentMethodType.Publish);
message.addAlternateView(app.requestApointment());
MapiMessage msg = MapiMessage.fromMailMessage(message);
// Save the appointment as a draft
msg.save(dataDir + "appointment-draft.msg");
יצירת פגישה בטיוטה מטקסט
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
String ical = "BEGIN:VCALENDAR\r\n"
+ "METHOD:PUBLISH\r\n"
+ "PRODID:-//Aspose Ltd//iCalender Builder (v3.0)//EN\r\n"
+ "VERSION:2.0\r\n"
+ "BEGIN:VEVENT\r\n"
+ "ATTENDEE;CN=attendee@domain.com:mailto:attendee@domain.com\r\n"
+ "DTSTART:20130220T171439\r\n"
+ "DTEND:20130220T174439\r\n"
+ "DTSTAMP:20130220T161439Z\r\n"
+ "END:VEVENT\r\n"
+ "END:VCALENDAR";
MailMessage message = new MailMessage("sender@domain.com", "recipient@domain.com", "", "");
AlternateView av = AlternateView.createAlternateViewFromString(ical, new ContentType("text/calendar"));
message.getAlternateViews().addItem(av);
MapiMessage msg = MapiMessage.fromMailMessage(message);
// Save the appointment as a draft
msg.save(dataDir + "DraftAppointment.msg");
הוספה והסרה של קבצים מצורפים מפריטי לוח שנה
Aspose.Email מספקת אוסף קבצים מצורפים שניתן להשתמש בו כדי להוסיף ולקבל קבצים מצורפים המשויכים לפריטי לוח שנה. מאמר זה מציג כיצד:
- צור והוסף קבצים מצורפים ל‑ Appointment אובייקט מחלקה.
- קבלת מידע על קבצים מצורפים מפגישה.
- חילוץ קבצים מצורפים מפגישה.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the resource directory.
String dataDir = "data/";
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2026, java.util.Calendar.DECEMBER, 1, 0, 0, 0);
Date endDate = calendar.getTime();
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(new MailAddress("attendee@domain.com", "Attendee"));
WeeklyRecurrencePattern recurrence = new WeeklyRecurrencePattern(3);
Appointment app = new Appointment("Appointment Location", "Appointment Summary",
"Appointment Description", startDate, endDate,
new MailAddress("organizer@domain.com", "Organizer"), attendees, recurrence);
// Attach a file from disk to this appointment
File file = new File(dataDir + "sample.xlsx");
try (FileInputStream fis = new FileInputStream(file)) {
app.getAttachments().addItem(new Attachment(fis, file.getName()));
}
String savedFile = "appWithAttachments.ics";
app.save(dataDir + savedFile, AppointmentSaveFormat.Ics);
// Load it back and extract the attachments
Appointment loaded = Appointment.load(dataDir + savedFile);
System.out.println("Total Attachments: " + loaded.getAttachments().size());
for (int i = 0; i < loaded.getAttachments().size(); i++) {
Attachment att = loaded.getAttachments().get_Item(i);
System.out.println(att.getName());
// Save the attachment to disk
att.save(dataDir + att.getName());
}
עיצוב פגישות
דגימות התכנות למטה מדגימות כיצד להשתמש ב‑ AppointmentFormattingOptions מחלקה לעיצוב טקסט ו‑HTML.
דוגמת תכנות - עיצוב טקסט
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "test.ics");
AppointmentFormattingOptions formattingOptions = new AppointmentFormattingOptions();
formattingOptions.setLocationFormat("Where: {0}");
formattingOptions.setTitleFormat("Subject: {0}");
formattingOptions.setDescriptionFormat("\r\n*~*~*~*~*~*~*~*~*~*\r\n{0}");
System.out.println(appointment.getAppointmentText(formattingOptions));
דוגמת תכנות - עיצוב HTML
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "test.ics");
AppointmentFormattingOptions formattingOptions = AppointmentFormattingOptions.createAsHtml();
formattingOptions.setLocationFormat("<FONT SIZE=2 FACE=\"Arial\"><b>Where: {0}</b></FONT><BR>");
formattingOptions.setTitleFormat("<FONT SIZE=2 FACE=\"Arial\"><b>Subject: {0}</b></FONT><BR>");
formattingOptions.setDescriptionFormat("<P><FONT SIZE=2 FACE=\"Arial\">-----------<br><i>{0}</i></FONT></P>");
System.out.println(appointment.getAppointmentText(formattingOptions));
המרת ICS ל‑MSG
ה‑API מאפשר להמיר בקלות פגישה לאובייקט הודעה. דוגמת הקוד הבאה מציגה כיצד להמיר בקשת פגישה ל‑MailMessage או MapiMessage:
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "appRequest.ics");
MailMessage eml = appointment.toMailMessage();
MapiMessage msg = appointment.toMapiMessage();
msg.save(dataDir + "appRequest.msg");
קביעת גרסת הפגישה
להגדרת גרסת פגישה, השתמשו ב‑ getVersion() שיטה של Appointment מחלקה. מסייעת לקבוע באיזו גרסה הקבצים מבוססים, ומבטיחה אינטגרציה עם מערכות ואפליקציות אחרות.
דוגמת הקוד הבאה מציגה כיצד להשתמש במתודה זו בפרויקט שלכם:
Appointment app = Appointment.load("data/meeting.ics");
// The version is a string that holds the ICS/VCS version, e.g. "2.0" for iCalendar
if ("2.0".equals(app.getVersion())) {
// do something
}
קריאת אירועים מרובים מקובץ ICS
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
List<Appointment> appointments = new ArrayList<Appointment>();
CalendarReader reader = new CalendarReader(dataDir + "US-Holidays.ics");
while (reader.nextEvent()) {
appointments.add(reader.getCurrent());
}
System.out.println("Number of events read: " + appointments.size());
// Reading events from a specific index
appointments = new ArrayList<Appointment>();
AppointmentLoadOptions options = new AppointmentLoadOptions();
options.setEventIndex(4);
// Start reading from the fifth event in the file
reader = new CalendarReader(dataDir + "US-Holidays.ics", options);
while (reader.nextEvent()) {
appointments.add(reader.getCurrent());
}
System.out.println("Number of events read from index 4: " + appointments.size());
כתיבת אירועים מרובים לקובץ ICS
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
AppointmentIcsSaveOptions saveOptions = new AppointmentIcsSaveOptions();
saveOptions.setAction(AppointmentAction.Create);
CalendarWriter writer = new CalendarWriter(dataDir + "WriteMultipleEventsToICS_out.ics", saveOptions);
// Set the start and end date of the meeting
java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2026, java.util.Calendar.JUNE, 19, 19, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2026, java.util.Calendar.JUNE, 19, 20, 0, 0);
Date endDate = calendar.getTime();
MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("recipient@domain.com"));
try {
for (int i = 0; i < 10; i++) {
Appointment app = new Appointment(
"Room 112", // location
startDate, // start time
endDate, // end time
new MailAddress("organizer@domain.com"), // organizer
attendees); // attendees
app.setDescription("Test body " + i);
app.setSummary("Test summary: " + i);
writer.write(app);
}
} finally {
writer.dispose();
}
הגדר סטטוס משתתפים של משתתפי פגישה
Aspose.Email for Java מאפשר לך להגדיר את מצב המשתתפים באפונטמנט בזמן בניית הודעת תגובה. פעולה זו מוסיפה את המאפיין PARTSTAT לקובץ ה‑ICS.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String location = "Room 5";
java.util.Calendar c = java.util.Calendar.getInstance();
c.set(2026, java.util.Calendar.DECEMBER, 10, 10, 12, 11);
Date startDate = c.getTime();
c.set(2026, java.util.Calendar.DECEMBER, 10, 13, 11, 12);
Date endDate = c.getTime();
MailAddress organizer = new MailAddress("organizer@domain.com", "Organizer");
MailAddress attendee1 = new MailAddress("first@domain.com", "First attendee");
MailAddress attendee2 = new MailAddress("second@domain.com", "Second attendee");
attendee1.setParticipationStatus(ParticipationStatus.Accepted);
attendee2.setParticipationStatus(ParticipationStatus.Declined);
MailAddressCollection attendees = new MailAddressCollection();
attendees.addMailAddress(attendee1);
attendees.addMailAddress(attendee2);
Appointment target = new Appointment(location, startDate, endDate, organizer, attendees);
התאמה של מזהה המוצר ל‑ICalendar
Aspose.Email for Java מאפשר לקבל או להגדיר את מזהה המוצר שייצר את אובייקט iCalendar. ניתן להגדיר אותו על פגישת האפונטמנט עצמה, או על AppointmentIcsSaveOptions בעת שמירה.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
Appointment appointment = Appointment.load(dataDir + "appointment.ics");
// Read the product identifier that created the iCalendar object
System.out.println("Product ID: " + appointment.getProductId());
// The identifier written to the output file can be set on the appointment ...
appointment.setProductId("-//My Company//My Product//EN");
// ... or on the save options
AppointmentIcsSaveOptions saveOptions = new AppointmentIcsSaveOptions();
saveOptions.setProductId("-//My Company//My Product//EN");
appointment.save(dataDir + "appointment_out.ics", saveOptions);
כיצד לעקוף אימות כתובת בעת ניסיון לטעון פגישות
Aspose.Email for Java מאפשר לעקוף את שגיאת אימות האימייל על‑ידי הגדרת ה‑ IgnoreSmtpAddressCheck אפשרות ב‑ AppointmentLoadOptions אובייקט והעברתו לקריאת הטעינה.
AppointmentLoadOptions lo = new AppointmentLoadOptions();
lo.setIgnoreSmtpAddressCheck(true);
Appointment appointment = Appointment.load("data/app.ics", lo);