`
lynen
  • 浏览: 125109 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

通过ical4j与javamail实现会议邀请总结

阅读更多

ical4j

http://sourceforge.net/projects/ical4j/files/

iCal4j is a Java library used to read and write iCalendar data streams as defined in RFC2445. The iCalendar standard provides a common data format used to store information about calendar-specific data such as events, appointments, to-do lists, etc. All of the popular calendaring tools, such as Lotus Notes, Outlook and Apple's iCal also support the iCalendar standard.

 

javamail

在使用javamail的时候,需要jaf的支持。javamail与jaf都可以直接登录oracle的网站下载得到

 

在通过ical4j与javamail实现会议邀请的时候,过程如下

1、通过ical4j创建一个Calendar,这个Calendar中可以包括VEvent、VAlarm、TODO等多项内容。而会议邀请则必须要包含VEvent,如果需要提醒,则可以包含VAlarm

2.、获取到Calendar后,将Calendar放入Message中通过javamail进行发送。在发送成功后,可能会碰到的问题有

   (1)乱码。问题解决方法很简单。可以将Calendar按照某种编码格式例如UTF-8转换byte后,通过byte[]构建Message所用到的InputStream进行发送(这个只是解决问题的思路)

   (2)通过foxmail接收到的会议邀请不能进行应答。。。QQ~~~这个问题偶没有耐心看了

    (3)通过outlook接收到的会议邀请是附件,而非outlook所支持的日历项(这个问题还需要区分outlook的版本,如果版本较低的话有可能接收到的不是附件,而是calendar的报文---outlook express 6.0就有这个问题)。对于附件的问题,翻译http://squirrelsewer.blogspot.com/2008/03/ical4j-javamail-exchange-and-outlook.html中的资料了,找到了解决方法。以下是关键代码

public MimeMessage createMimeMessage(Session session) throws Exception {   
MimeMessage mimeMessage = new MimeMessage(session);   
mimeMessage.setSubject(getSubject());   
mimeMessage.addFrom(from);   
mimeMessage.addRecipients(Message.RecipientType.TO, to);   
//以下两步骤的处理很重要,可以避免outlook将生成的日历项当做附件处理   
Multipart multipart = new MimeMultipart();   
MimeBodyPart iCalAttachment = new MimeBodyPart();   
byte[] invite = createICalInvitation(getMeetingID(), getSubject(), getContent(), getMeetingStart(), getMeetingEnd(), getMeetingTimeZone());   
/*setDataHandler的处理方式也是非常关键  
如果直接按照处理  
mimeMessage.setContent(iCalAttachment , "text/calendar");  
则在邮件发送的时候会抛出如下异常  
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/calendar  
这样发送能否成功目前没有找到解决方案  
*/  
iCalAttachment.setDataHandler(new DataHandler(new ByteArrayDataSource(new ByteArrayInputStream(invite), "text/calendar;method=REQUEST;charset=\"UTF-8\"")));   
multipart.addBodyPart(iCalAttachment);   
mimeMessage.setContent(multipart);   
return mimeMessage;   
}   
  
private byte[] createICalInvitation(String _meetingID, String _subject, String _content, Date _start, Date _end, TimeZone _tz) throws Exception {   
/**  
以下两步骤的处理也是为了防止outlook或者是notes将日历当做附件使用增加的*/  
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_OUTLOOK_COMPATIBILITY, true);   
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_NOTES_COMPATIBILITY, true);   
  
VEvent vEvent = new VEvent();   
vEvent.getProperties().add(new Uid(_meetingID));   
vEvent.getProperties().add(new Summary(_subject));   
vEvent.getProperties().add(new Description(_content));   
vEvent.getProperties().add(new DtStart(new DateTime(_start)));   
vEvent.getProperties().add(new DtEnd(new DateTime(_end)));   
  
Calendar cal = new Calendar();   
cal.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));   
cal.getProperties().add(Version.VERSION_2_0);   
cal.getProperties().add(CalScale.GREGORIAN);   
cal.getProperties().add(Method.REQUEST);   
TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();   
VTimeZone tz = registry.getTimeZone(_tz.getID()).getVTimeZone();   
cal.getComponents().add(tz);   
cal.getComponents().add(vEvent);   
  
//如果邮件得到的日历内容时乱码,可以考虑通过将日历内容按照某种编码转换成bytes后,在生成stream   
ByteArrayOutputStream bout = new ByteArrayOutputStream();   
CalendarOutputter outputter = new CalendarOutputter();   
outputter.output(cal, bout);   
return bout.toByteArray();   
}  

  

 

 

0
0
分享到:
评论
1 楼 cfyme 2012-03-05  
请问一下,我成功发送了一个会议邀请,怎么取消会议邀请,怎么更改会议邀请

相关推荐

Global site tag (gtag.js) - Google Analytics