ทำงานกับนัดหมาย
โหลดและบันทึกการนัดหมายในรูปแบบ 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. โปรดดูชื่อ accessor: ใน 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 อนุญาตให้แปลง Appointment เป็นออบเจกต์ข้อความ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแปลงคำขอการนัดหมายเป็น 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 ออบเจกต์และส่งผ่านมันไปยังการเรียก load.
AppointmentLoadOptions lo = new AppointmentLoadOptions();
lo.setIgnoreSmtpAddressCheck(true);
Appointment appointment = Appointment.load("data/app.ics", lo);