Saturday, January 22, 2005

Outlook calendar appointment email with Java

This week I have been working on figuring out how to send an Outlook calendar appointment email with Java. I looked at a lot of different websites and it seemed like a lot of people are asking how to do this and no one is really giving any kind of solution. I use the web all of the time to answer questions, so I figured that since I had figured this out I would contribute back to the general knowledge base. Matt Bess also helped me in figuring this out.

What I have determined is that if you want to send an Outlook calendar appointment using Java, you have to send it as either a .vcs or .ics attachment. All the receipient has to do is double click on the attachment and it will be put in their calendar. Initially I wanted to have the behavior mimic that of a native Outlook appontment by giving the "Accept" or "Decline" buttons. Whenever I tried to send the email I would get the following error:

javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/calendar

This happens because there is not a mail handler defined in my mailcap file. But as far as I know there is not a java mail handler for Outlook messages. So the way to get around this problem is to send a multipart message. The first part is the content of the message that will display in the email client. This nice thing about doing it this way is that you can still have a HTML formatted email as the message and then attach the appointment for their calendar. Using the multipart gives a lot of flexibility. Here is some sample code:


MimeMessage message = mailSender.createMimeMessage();

message.setFrom( new InternetAddress("jared.blake@gmail.com", "jared.blake@gmail.com") );
javax.mail.Address address = new InternetAddress("jared.blake@gmail.com", "jared.blake@gmail.com");
message.addRecipient(MimeMessage.RecipientType.TO, address);
message.setSubject("test");

String contentType = "text/plain";

StringBuffer buffer = new StringBuffer();

buffer.append("BEGIN:VCALENDAR\n" +
  "PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN\n" +
  "VERSION:1.0\n" +
  "BEGIN:VEVENT\n" +
  "ATTENDEE:MAILTO:jared.blake@gmail.com\n" +
  "DTSTART:20050120T170000Z\n" +
  "DTEND:20050120T180000Z\n" +
  "LOCATION;ENCODING=QUOTED-PRINTABLE:Lindon, UT\n" +
  "UID:040000008200E00074C5B7101A82E008000000004024D79F7EFDC4010000000000000000100\n" +
  " 00000CA06303EA8D5AD408E07455F234F0294\n" +
  "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:This is a test calendar item.=0D=0A\n" +
  "SUMMARY;ENCODING=QUOTED-PRINTABLE:My Event\n" +
  "PRIORITY:3\n" +
  "END:VEVENT\n" +
  "END:VCALENDAR");

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText("Here's the file");

// Create a Multipart
Multipart multipart = new MimeMultipart();

// Add part one
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
// Create second body part
messageBodyPart = new MimeBodyPart();
String filename = "test calendar.vcs";
messageBodyPart.setFileName(filename);
messageBodyPart.setContent(buffer.toString(), "text/plain");

// Add part two
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// send message
mailSender.send(message);


This code should give you a basic idea. You can find a lot of different sites that explain how to use the vCal format. I found this page to have some good links.

http://wmf.editthispage.com/discuss/msgReader$7900?mode=day

Like I said, I know a lot of people are looking to figure out how to send Outlook calendar appointments, so I hope this has helped get you started. If you have questions I will try to answer them the best I can.

17 comments:

Anonymous said...

is it definite that you can't send outlook appointments w/o an attachment?

Jared said...

As far as I know there isn't a java datatype handler for the mime type "text/calendar", which is what Outlook uses. This mime type is what tells Outlook it is receiving an appointment message and it handles it differently. Even when I tried to spoof the handler and mapped the text data handler to that mime type, Outlook didn't recogize it as an appointment. A text/calendar handler could possibly exist, but I wasn't able to find one.

Anonymous said...

When I used this code I got the following error. How this can be addressed.

javax.mail.MessagingException: Error in input stream;
nested exception is:
java.io.IOException: Stream closed

void javax.mail.internet.InternetHeaders.load(java.io.InputStream)

void javax.mail.internet.InternetHeaders.init(java.io.InputStream)

javax.mail.internet.InternetHeaders javax.mail.internet.MimeMessage.createInternetHeaders(java.io.InputStream)

void javax.mail.internet.MimeMessage.parse(java.io.InputStream)

void javax.mail.internet.MimeMessage.init(javax.mail.Session, java.io.InputStream)

void ex.OutlookMeeding.main(java.lang.String[])

Jared said...

It looks like whatever input stream you are using to create your message is closing before everything gets created. That is my guess. To confirm that it is a problem with your file, try using a statically defined string instead of streaming from a file. If the message works from that, then you know there is a problem with you file streaming. If you want to post the code that is causing the error, I will see if I can figure something out. Hope this helps.

Anonymous said...

How to specify multiple attendees?

Anonymous said...

I tried using another ATTENDEE:MAILTO:. But it did not work for me. Attendees list shows only one user who ever receives the mail, mentioning him/her as meeting organizer.

Jared said...

If you look at the iCal standards doc (http://www.ietf.org/rfc/rfc2445.txt) it mentions that some attributes can have multiple values separated by commas. I haven't had a chance to try it with the ATTENDEE attribute, but that would be my guess.

Anonymous said...

is there any way , can send an appointment without attachment? User complains that double click


Thank you

Anonymous said...

Dear jared, Are you thinking of something like at www.hob.com - to add a concert per se? I'm try to do the same. I'm probably not nearly as savvy as you, but if you have any isight, I'd sure be grateful! Thanks, Daniel (beast0117@hotmail.com)http://www.hob.com/community/remindme/outlook.asp?s=sch&id=34176

Sid Rules said...

hi jared ,
I developed an application using Java which is as
By clicking Conference Tab it open the conferecne page and on this page i can add all the details regarding conferece and here i can add user to whom i want to invite and by clicking SEND button i can send email to the user using OUTLOOK here all conference data get mapped to the OUTLOOK.
On the user side when open this mail they get notify
But now i want to modify this such as when the user get emails and when the opend this it should ask them to update there calendar and if they say yes the conference details must reflected to calendar and calendar must get update by giving this facility there is no need to that user to always see when is the conferece coz on updataing there calendar it will give them popoup's.
Can it be possible to convert my conferece datails to convert them to .vcs or .ics and can i attach them.

Thanks & Regards,

siddharth.wanjare@gmail.com

Sid Rules said...

hi jared ,
I developed an application using Java which is as
By clicking Conference Tab it open the conferecne page and on this page i can add all the details regarding conferece and here i can add user to whom i want to invite and by clicking SEND button i can send email to the user using OUTLOOK here all conference data get mapped to the OUTLOOK.
On the user side when open this mail they get notify
But now i want to modify this such as when the user get emails and when the opend this it should ask them to update there calendar and if they say yes the conference details must reflected to calendar and calendar must get update by giving this facility there is no need to that user to always see when is the conferece coz on updataing there calendar it will give them popoup's.
Can it be possible to convert my conferece datails to convert them to .vcs or .ics and can i attach them.

Thanks & Regards,

siddharth.wanjare@gmail.com

Unknown said...

Hi Jared

I am wondering if you are missing the creation of mailSender instance in your code . I see the instance variable creation , but not the Instance creation.

Thanks
Sreenath

Unknown said...

Hi Jared

I am wondering if you are missing the creation of mailSender instance in your code . I see the instance variable creation , but not the Instance creation.

Thanks
Sreenath

Ramesh said...

I read out all the code..but can any body tell me how to send a meeting request directly..without sending as a attachment..please

Ramesh said...

can any body tell me to send a meeting request in java directly...without sending it as attachments

Call of HOPE said...

Could you please say what is this mailSender object is??? its urgent

Thanks in Advance
Sriram

Jared said...

I have posted an update to this post. You can find it here