Sunteți pe pagina 1din 4

Java Outlook Calendar Invite

Java Mail is equipped with simpler methods to create and send emails. But unfortunately the API support
given to send an Outlook Calendar Invite is very limited as it was not originally introduced for it. So you
need to use the same method for creating and sending an Invite, but in a different way. The message
body part to be formed in such a way that the outlook can recognize the message as an Invite. Also the
date set with the Invite is considered as EST time zone by default and the same needs to be converted to
other zones if required. Below points should be kept in mind while creating a component for sending a
Calendar Invite.
1) The below headers need to be set to the message body part-
messageBodyPart.setHeader("Content-Class","urn:content-classes:calendarmessage");
messageBodyPart.setHeader("Content-ID", "calendar_message");

2) Set the mime type as text/calendar
3) Begin the message body part with BEGIN:VCALENDAR
4) The calendar Invite name can be set in the header as below
message.setHeader ("X-Mailer", "Test-Mailer");

The CalenderInvite class (attached below) can be reused to send an Invite by calling its send() method.
The send(), method expects the following parameters to be passed with it.
fromDate date and time when the meeting starts (java.util.Date)
Date toDate -- date and time when the meeting ends (java.util.Date)
ConfRoom -- name of conference room (java.lang.String)
toList email ids of Invitees in comma separated form (java.lang.String)
from email id of the organizer (java.lang.String)
Subject subject for the Invite (java.lang.String)
Desc-- message content in the meeting Invite (java.lang.String)
hostAddress-- exchange server address (java.lang.String)
The source code is attached here
CalenderInvite.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class CalendarInvite {

public boolean send(Date fromDate, Date toDate, String confRoom,
String toList, String from, String Subject, String desc,
String hostAddress) throws Exception {

String uniqueId = System.nanoTime() + "";

toList += ("," + from);
String meetingStartTime = getDateinCalendarFormat(fromDate);
String meetingEndTime = getDateinCalendarFormat(toDate);
Properties prop = new Properties();
StringBuffer sb = new StringBuffer();
StringBuffer buffer = null;
Session session = Session.getDefaultInstance(prop, null);
Message message = new MimeMessage(session);

try {
prop.put("mail.smtp.host", hostAddress);

// Define message
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toList, false));
message.setSubject(Subject);
message.setHeader("X-Mailer", "TD-Mailer");
// Create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
buffer = sb
.append("BEGIN:VCALENDAR\n"
+ "PRODID:-//Microsoft Corporation//Outlook 9.0
MIMEDIR//EN\n"
+ "VERSION:2.0\n" + "METHOD:REQUEST\n"
+ "BEGIN:VEVENT\n"
+ "ATTENDEE;ROLE=REQ-
PARTICIPANT;RSVP=TRUE:MAILTO:"
+ toList
+ "\n"
+ "ORGANIZER:MAILTO:"
+ from
+ "\n"
+ "DTSTART:"
+ meetingStartTime
+ "\n"
+ "DTEND:"
+ meetingEndTime
+ "\n"
+ "LOCATION:"
+ confRoom
+ "\n"
+ "TRANSP:OPAQUE\n"
+ "SEQUENCE:0\n"
+ "UID:"
+ uniqueId
+ "@iquest.com\n"
+ "DTSTAMP:"
+ meetingEndTime
+ "\n"
+ "CATEGORIES:Meeting\n"
+ "DESCRIPTION:"
+ desc
+ ".\n\n"
+ "SUMMARY:"
+ ""
+ "\n"
+ "PRIORITY:1\n"
+ "CLASS:PUBLIC\n"
+ "BEGIN:VALARM\n"
+ "TRIGGER:PT1440M\n"
+ "ACTION:DISPLAY\n"
+ "DESCRIPTION:Reminder\n"
+ "END:VALARM\n" + "END:VEVENT\n" +
"END:VCALENDAR");
// messageBodyPart.setFileName("TestMeeting.ics");
messageBodyPart
.setDataHandler(new DataHandler(new ByteArrayDataSource(
buffer.toString(), "text/calendar")));
messageBodyPart.setHeader("Content-Class",
"urn:content-classes:calendarmessage");
messageBodyPart.setHeader("Content-ID", "calendar_message");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);

}
// Catching only calendar invite exceptions. Other functionalities be
// still working
catch (Exception me) {
// log exception here
}
return false;
}

public String getDateinCalendarFormat(Date date) {
DateFormat requiredFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, -5);
cal.add(Calendar.MINUTE, -30);
date = cal.getTime();
return requiredFormat.format(date) + "Z";
}

private class ByteArrayDataSource implements DataSource {
private byte[] data; // data for mail message

private String type; // content type/mime type

ByteArrayDataSource(String data, String type)
throws UnsupportedEncodingException {
// Assumption that the string contains only ascii
// characters ! Else just pass in a charset into this
// constructor and use it in getBytes()
this.data = data.getBytes("iso-8859-1");
this.type = type;
}

// DataSource interface methods
public InputStream getInputStream() throws IOException {
if (data == null)
throw new IOException(
"no data exception in ByteArrayDataSource");
return new ByteArrayInputStream(data);
}

public OutputStream getOutputStream() throws IOException {
throw new IOException("illegal operation in ByteArrayDataSource");
}

public String getContentType() {
return type;
}

public String getName() {
return "dummy";
}
}

}

S-ar putea să vă placă și