MimetypesFileTypeMap mimetypes = (MimetypesFileTypeMap)MimetypesFileTypeMap.getDefaultFileTypeMap();
mimetypes.addMimeTypes("text/calendar ics ICS");
//register the handling of text/calendar mime type
MailcapCommandMap mailcap = (MailcapCommandMap) MailcapCommandMap.getDefaultCommandMap();
mailcap.addMailcap("text/calendar;; x-java-content-handler=com.sun.mail.handlers.text_plain");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
// Create an alternative Multipart
Multipart multipart = new MimeMultipart("alternative");
//part 1, html text
BodyPart messageBodyPart = buildHtmlTextPart();
multipart.addBodyPart(messageBodyPart);
// Add part two, the calendar
BodyPart calendarPart = buildCalendarPart();
multipart.addBodyPart(calendarPart);
//Put the multipart in message
message.setContent(multipart);
// send the message
Transport transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
private BodyPart buildHtmlTextPart() throws MessagingException {
MimeBodyPart descriptionPart = new MimeBodyPart();
//Note: even if the content is spcified as being text/html, outlook won't read correctly tables at all
// and only some properties from div:s. Thus, try to avoid too fancy content
String content = "simple meeting invitation";
descriptionPart.setContent(content, "text/html; charset=utf-8");
return descriptionPart;
}
//define somewhere the icalendar date format
private static SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");
private BodyPart buildCalendarPart() throws Exception {
BodyPart calendarPart = new MimeBodyPart();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 1);
Date start = cal.getTime();
cal.add(Calendar.HOUR_OF_DAY, 3);
Date end = cal.getTime();
//check the icalendar spec in order to build a more complicated meeting request
String calendarContent =
"BEGIN:VCALENDAR\n" +
"METHOD:REQUEST\n" +
"PRODID: BCP - Meeting\n" +
"VERSION:2.0\n" +
"BEGIN:VEVENT\n" +
"DTSTAMP:" + iCalendarDateFormat.format(start) + "\n" +
"DTSTART:" + iCalendarDateFormat.format(start)+ "\n" +
"DTEND:" + iCalendarDateFormat.format(end)+ "\n" +
"SUMMARY:test request\n" +
"UID:324\n" +
"ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:organizer@yahoo.com\n" +
"ORGANIZER:MAILTO:organizer@yahoo.com\n" +
"LOCATION:on the net\n" +
"DESCRIPTION:learn some stuff\n" +
"SEQUENCE:0\n" +
"PRIORITY:5\n" +
"CLASS:PUBLIC\n" +
"STATUS:CONFIRMED\n" +
"TRANSP:OPAQUE\n" +
"BEGIN:VALARM\n" +
"ACTION:DISPLAY\n" +
"DESCRIPTION:REMINDER\n" +
"TRIGGER;RELATED=START:-PT00H15M00S\n" +
"END:VALARM\n" +
"END:VEVENT\n" +
"END:VCALENDAR";
calendarPart.addHeader("Content-Class", "urn:content-classes:calendarmessage");
calendarPart.setContent(calendarContent, "text/calendar;method=CANCEL");
return calendarPart;
}