DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 106 posts at DZone. View Full User Profile

Create, Add & Retrieve Email Attachments from Calendar Items in Java Apps

04.16.2014
| 21463 views |
  • submit to reddit
//Sample Code for Adding and Retrieving Attachments from Calendar Items

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(2012, Calendar.NOVEMBER, 1, 0, 0, 0);
Date startDate = calendar.getTime();
calendar.set(2012, Calendar.DECEMBER, 1);
Date endDate = calendar.getTime();

MailAddressCollection attendees = new MailAddressCollection();
attendees.add(new MailAddress("attendee_address@domain.com", "Attendee"));
WeeklyRecurrencePattern expected = new WeeklyRecurrencePattern(3);

Appointment app = new Appointment("Appointment Location", "Appointment Summary", "Appointment Description",
									startDate, endDate,
									new MailAddress("organizer_address@domain.com", "Organizer"), attendees, expected);

//Attach a file from disc to this appointment
File file = new File("OutputXLS.xls");
FileInputStream fis = new FileInputStream(file);

Attachment att = new Attachment(fis, file.getName());
app.getAttachments().add(att);
fis.close();
String savedFile = "appWithAttachments.ics";
app.save(savedFile, AppointmentSaveFormat.Ics);

Appointment app2 = Appointment.load(savedFile);
System.out.println("Total Attachments: "  + app2.getAttachments().size());
for (int i=0; i< app2.getAttachments().size();i++)
{
	att = app2.getAttachments().get(i);
	System.out.println(att.getName());

        //Save the attachment to disc
        att.save(att.getName());
}
 
This technical tip enables Java developers to create, add & retrieve attachments from calendar items in Java Apps using Aspose.Email for Java. Aspose.Email lets developers manage attachments with email messages using the MailMessage and MapiMessage class. Microsoft Outlook allows you to add as well as retrieve attachments from calendar items, that is ICS. This article shows how the same can be achieved using Aspose.Email for Java for ICS files. Aspose.Email provides an attachments collection that can be used to add and retrieve attachments associated with calendar items. This article shows how to:

  • Create and add attachments to an Appointment class object.
  • Retrieve attachments information an appointment.
  • Extract attachments from an appointment.