如何使用Java Mail发送iCal会议请求,并接收响应 [英] How to send an iCal meeting request using Java Mail, and receive the response
问题描述
我正在使用Java Mail Library发送一个iCal,我已经阅读了问题,我已经有一些示例代码
public class SendMeetingRequest {
String host =;
String port =;
String sender =;
public static SimpleDateFormat dateFormat = new SimpleDateFormat(yyyyMMdd'T'HHmm'00);
public static SimpleDateFormat dateParser = new SimpleDateFormat(dd-MM-yyyy HH:mm);
public static void main(String [] args)throws Exception {
SendMeetingRequest sender = new SendMeetingRequest();
sender.sendInvitation(LogicaCMG Inschrijf Site
,new String [] {robert< dot> willems< dot> of< dot> brilman< at> logicacmg< dot> com
}
,使用JavaMail的Outlook会议请求
,dateParser.parse(28-08-2006 18:00)
,dateParser.parse(28-08 -2006 21:00)
,LIS-42
,bar op scheveningen
,< font color = \Red\> Aanwezigheid verplicht! < / font>< br>我们的老板leenkenenenenenen遇见了z'n allen。
);
void sendInvitation(String organizer
,String [] to
,String subject
,Date start
,日期结束
,String invitationId
,字符串位置
,字符串描述
)throws异常{
try {
属性prop = new Properties ();
prop.put(mail.smtp.port,port);
prop.put(mail.smtp.host,主机);
会话session = Session.getDefaultInstance(prop);
session.setDebug(true);
//定义消息
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
// Set To
if(to!= null&&(to.length> 0)){
InternetAddress [] address = new InternetAddress [长度 ] ; (int i = 0; i< to.length; i ++){
address [i] = new InternetAddress(to [i]);
}
message.setRecipients(Message.RecipientType.TO,address);
}
//设置主题
message.setSubject(subject);
//创建iCalendar消息
StringBuffer messageText = new StringBuffer();
messageText.append(BEGIN:VCALENDAR\\\
+
PRODID: - // Microsoft Corporation // Outlook 9.0 MIMEDIR // EN\\\
+
VERSION:2.0 \\\
+
METHOD:REQUEST\\\
+
BEGIN:VEVENT\\\
+
ORGANIZER:MAILTO:);
messageText.append(organizer);
messageText.append(\\\
+
DTSTART:);
messageText.append(dateFormat.format(start));
messageText.append(\\\
+
DTEND:);
messageText.append(dateFormat.format(end));
messageText.append(\\\
+
LOCATION:);
messageText.append(location);
messageText.append(\\\
+
UID:);
messageText.append(invitationId);
messageText.append(\\\
+
DTSTAMP:);
messageText.append(dateFormat.format(new java.util.Date()));
messageText.append(\\\
+
DESCRIPTION; ALTREP = \CID:< eventDescriptionHTML> \);
messageText.append(\\\
+
BEGIN:VALARM\\\
+
TRIGGER:-PT15M\\\
+
ACTION:DISPLAY\\\
+
说明:提醒\\\
+
END:VALARM\\\
+
END:VEVENT\\\
+
END:VCALENDAR
);
Multipart mp = new MimeMultipart();
MimeBodyPart meetingsPart = new MimeBodyPart();
meetingsPart.setDataHandler(new DataHandler(new StringDataSource(messageText.toString() / calendar,meetingRequest)));
mp.ad dBodyPart(meetingPart);
MimeBodyPart descriptionPart = new MimeBodyPart();
descriptionPart.setDataHandler(new DataHandler(new StringDataSource(description,text / html,eventDescription)));
descriptionPart.setContentID(< eventDescriptionHTML>);
mp.addBodyPart(descriptionPart);
message.setContent(mp);
//发送消息
Transport.send(message);
} catch(MessagingException me){
me.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
}
私有静态类StringDataSource实现DataSource {
private String contents;
private String mimetype;
private String name;
public StringDataSource(String contents
,String mimetype
,String name
){
this.contents = contents;
this.mimetype = mimetype;
this.name = name;
}
public String getContentType(){
return(mimetype);
}
public String getName(){
return(name);
}
public InputStream getInputStream(){
return(new StringBufferInputStream(contents));
}
public OutputStream getOutputStream(){
throw new IllegalAccessError(此数据源无法写入);
}
}}
但它作为附件发送到展望2007和outlook 2003,甚至如果我点击附件查看和接受,我没有收到答案,这是应用程序的目的有类似的功能,如outlook。
是否有任何需要知道的程序,还是邀请ID使事情变得粗糙?
p>所以我解决了这个问题,这里是我发现的:
1 - 你必须更新到Java Mail API 1.4.2,使一切正常工作p>
2 - 写下您的消息的文字/日历部分,如下所示:
package com.xx.xx;
import java.util.Properties;
import javax.mail。*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
public class Email {
public Email(){
}
/ *
* @param args
* /
public static void main(String [] args){
try {
Email email = new Email();
email.send();
} catch(Exception e){
e.printStackTrace();
}
}
public void send()throws异常{
try {
String from =xx@xx.com ;
String to =xx@xx.com;
属性prop = new Properties();
prop.put(mail.smtp.host,mailhost);
会话session = Session.getDefaultInstance(prop,null);
//定义消息
MimeMessage message = new MimeMessage(session);
message.addHeaderLine(method = REQUEST);
message.addHeaderLine(charset = UTF-8);
message.addHeaderLine(component = VEVENT);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(使用JavaMail的Outlook会议请求);
StringBuffer sb = new StringBuffer();
StringBuffer buffer = sb.append(BEGIN:VCALENDAR\\\
+
PRODID: - // Microsoft Corporation // Outlook 9.0 MIMEDIR // EN\\\
+
VERSION:2.0\\\
+
METHOD:REQUEST\\\
+
BEGIN:VEVENT\\\
+
ATTENDEE; ROLE = REQ-PARTICIPANT ; RSVP = TRUE:MAILTO:xx@xx.com\\\
+
ORGANIZER:MAILTO:xx@xx.com\\\
+
DTSTART:20051208T053000Z\\\
+
DTEND:20051208T060000Z\\\
+
位置:会议室\+
TRANSP:OPAQUE\\\
+
SEQUENCE:0\\\
+
UID:040000008200E00074C5B7101A82E00800000000002FF466CE3AC5010000000000000000100\\\
+
000004377FE5C37984842BF9440448399EB02\\\
+
DTSTAMP:20051206T120102Z \\ n+
类别:会议\+
说明:这个会议的描述。\\\
\
摘要:测试会议请求\\ n+
优先权:5\\\
+
CLASS:PUBLIC\\\
+
BEGIN:VALARM\\\
+
TRIGGER: PT1440M\\\
+
ACTION:DISPLAY\\\
+
说明:Reminder\\\
+
END:VALARM\\\
+
END:VEVENT\\\
+
END:VCALENDAR);
//创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
//填写消息
messageBodyPart.setHeader(Content-Class,urn:content- classes:calendarmessage);
messageBodyPart.setHeader(Content-ID,calendar_message);
messageBodyPart.setDataHandler(new DataHandler(
new ByteArrayDataSource(buffer.toString(),text / calendar))); //非常重要
//创建一个Multipart
Multipart multipart = new MimeMultipart();
//添加第一部分
multipart.addBodyPart(messageBodyPart);
//将零件放入消息
message.setContent(multipart);
//发送消息
Transport.send(message);
} catch(MessagingException me){
me.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
}
}
3 - 替换您的变量,你很好去!
I'm trying to send an iCal to an outlook, using Java Mail Library, I've read already the Question, and I already have some sample code
public class SendMeetingRequest {
String host = "" ;
String port = "" ;
String sender = "" ;
public static SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd'T'HHmm'00'" ) ;
public static SimpleDateFormat dateParser = new SimpleDateFormat( "dd-MM-yyyy HH:mm" ) ;
public static void main(String[] args) throws Exception {
SendMeetingRequest sender = new SendMeetingRequest() ;
sender.sendInvitation( "LogicaCMG Inschrijf Site"
, new String[] { "robert<dot>willems<dot>of<dot>brilman<at>logicacmg<dot>com"
}
, "Outlook Meeting Request Using JavaMail"
, dateParser.parse( "28-08-2006 18:00″ )
, dateParser.parse( "28-08-2006 21:00″ )
, "LIS-42″
, "Bar op scheveningen"
, "<font color=\"Red\">Aanwezigheid verplicht!</font><br>We gaan lekker een biertje drinken met z’n allen."
) ;
}
void sendInvitation( String organizer
, String[] to
, String subject
, Date start
, Date end
, String invitationId
, String location
, String description
) throws Exception {
try {
Properties prop = new Properties();
prop.put("mail.smtp.port", port );
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 ) ;
}
// Set subject
message.setSubject(subject);
// Create iCalendar message
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"
) ;
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);
} catch (MessagingException me) {
me.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static class StringDataSource implements DataSource {
private String contents ;
private String mimetype ;
private String name ;
public StringDataSource( String contents
, String mimetype
, String name
) {
this.contents = contents ;
this.mimetype = mimetype ;
this.name = name ;
}
public String getContentType() {
return( mimetype ) ;
}
public String getName() {
return( name ) ;
}
public InputStream getInputStream() {
return( new StringBufferInputStream( contents ) ) ;
}
public OutputStream getOutputStream() {
throw new IllegalAccessError( "This datasource cannot be written to" ) ;
}
} }
But its being sent as an attachment to the outlook 2007 and outlook 2003, and even If I click the attachment to view and accept, I don't receive the Answer, which is the purpose of the application to have a similar functionality like outlook.
Is there any procedure I need to know of, or is it the Invitation ID that makes the thing rough?
So I solved the problem, and here is what I found:
1 - You have to update to Java Mail API 1.4.2 to make everything works fine
2 - Write the text/calendar part of your message like the following:
package com.xx.xx;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
public class Email {
public Email() {
}
/*
* @param args
*/
public static void main(String[] args) {
try {
Email email = new Email();
email.send();
} catch (Exception e) {
e.printStackTrace();
}
}
public void send() throws Exception {
try {
String from = "xx@xx.com";
String to = "xx@xx.com";
Properties prop = new Properties();
prop.put("mail.smtp.host", "mailhost");
Session session = Session.getDefaultInstance(prop, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.addHeaderLine("method=REQUEST");
message.addHeaderLine("charset=UTF-8");
message.addHeaderLine("component=VEVENT");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Outlook Meeting Request Using JavaMail");
StringBuffer sb = new StringBuffer();
StringBuffer 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:xx@xx.com\n" +
"ORGANIZER:MAILTO:xx@xx.com\n" +
"DTSTART:20051208T053000Z\n" +
"DTEND:20051208T060000Z\n" +
"LOCATION:Conference room\n" +
"TRANSP:OPAQUE\n" +
"SEQUENCE:0\n" +
"UID:040000008200E00074C5B7101A82E00800000000002FF466CE3AC5010000000000000000100\n" +
" 000004377FE5C37984842BF9440448399EB02\n" +
"DTSTAMP:20051206T120102Z\n" +
"CATEGORIES:Meeting\n" +
"DESCRIPTION:This the description of the meeting.\n\n" +
"SUMMARY:Test meeting request\n" +
"PRIORITY:5\n" +
"CLASS:PUBLIC\n" +
"BEGIN:VALARM\n" +
"TRIGGER:PT1440M\n" +
"ACTION:DISPLAY\n" +
"DESCRIPTION:Reminder\n" +
"END:VALARM\n" +
"END:VEVENT\n" +
"END:VCALENDAR");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setHeader("Content-Class", "urn:content- classes:calendarmessage");
messageBodyPart.setHeader("Content-ID", "calendar_message");
messageBodyPart.setDataHandler(new DataHandler(
new ByteArrayDataSource(buffer.toString(), "text/calendar")));// very important
// Create a Multipart
Multipart multipart = new MimeMultipart();
// Add part one
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// send message
Transport.send(message);
} catch (MessagingException me) {
me.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3 - Replace your variable, and you are good to go!
这篇关于如何使用Java Mail发送iCal会议请求,并接收响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!