我尝试在Outlook(2013)中发送日历邀请,但是作为附件文件发送而不是使用java发送邀请。
Properties prop = new Properties();
prop.put("mail.smtp.port", port );
prop.put("mail.smtp.auth", "false");
prop.put("mail.smtp.host", host );
Session session = Session.getDefaultInstance(prop);
session.setDebug(true);
//Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
//Set TO
if( to != null && ( to.length > 0 ) ) {
InternetAddress[] address = new InternetAddress[ to.length ] ;
for( int i = 0; i < to.length; i++ ) {
address[ i ] = new InternetAddress( to[ i ] ) ;
}
message.setRecipients( Message.RecipientType.TO, address ) ;
}
message.setSubject(subject);
StringBuffer messageText = new StringBuffer();
messageText.append("BEGIN:VCALENDAR\n" +
"PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n" +
"VERSION:2.0\n" +
"METHOD:REQUEST\n" +
"BEGIN:VEVENT\n" +
"ORGANIZER:MAILTO:" ) ;
messageText.append( organizer ) ;
messageText.append( "\n" + "DTSTART:");
messageText.append( dateformat.format( start ) ) ;
messageText.append( "\n" + "DTEND:" ) ;
messageText.append( dateformat.format( end ) ) ;
messageText.append( "\n" + "LOCATION:" ) ;
messageText.append( location ) ;
messageText.append( "\n" + "UID:" ) ;
messageText.append( invitationId ) ;
messageText.append( "\n" + "DTSTAMP:" ) ;
messageText.append( dateformat.format( new java.util.Date() ) ) ;
messageText.append( "\n" + "DESCRIPTION;ALTREP=\"CID:<eventDescriptionHTML>\"" ) ;
messageText.append( "\n" +
"BEGIN:VALARM\n" +
"TRIGGER:-PT15M\n" +
"ACTION:DISPLAY\n" +
"DESCRIPTION:Reminder\n" +
"END:VALARM\n" +
"END:VEVENT\n" +
"END:VCALENDAR"
) ;
MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
mc.addMailcap("text/calendar;;x-java-content-
handler=com.sun.mail.handlers.text_plain");
CommandMap.setDefaultCommandMap(mc);
MimetypesFileTypeMap mimetypes = (MimetypesFileTypeMap)MimetypesFileTypeMap.getDefaultFileTypeMap();
mimetypes.addMimeTypes("text/calendar ics ICS");
MimetypesFileTypeMap.setDefaultFileTypeMap(mimetypes);
BodyPart calendarPart = new MimeBodyPart();
calendarPart.addHeader("Content-Class", "urn:content-classes:calendarmessage");
calendarPart.setContent(messageText.toString(), "text/calendar;method=CANCEL");
Multipart mp = new MimeMultipart();
MimeBodyPart meetingPart = new MimeBodyPart() ;
meetingPart.setDataHandler( new DataHandler( new StringDataSource( messageText.toString(), "text/calendar", "meetingRequest" ) ) ) ;
mp.addBodyPart( meetingPart ) ;
MimeBodyPart descriptionPart = new MimeBodyPart() ;
descriptionPart.setDataHandler( new DataHandler( new StringDataSource(
description, "text/html", "eventDescription" ) ) ) ;
descriptionPart.setContentID( "<eventDescriptionHTML>" ) ;
mp.addBodyPart( descriptionPart ) ;
message.setContent( mp ) ;
// send message
Transport.send(message);