如何使用 BiWeekly 库和 Java 邮件 API 创建并发送更新到现有事件?
How to create and send update to existing event by uisng BiWeekly library and Java Mail API?
我正在使用 BiWeekly library to create VEVENT and then send it by using Java Mail API。一切正常,但我如何为现有事件创建更新,即当我收到更新的 VEVENT 时,它不会在日历中创建新事件但会更新现有事件?
我试过按以下方式设置顺序 event.setSequence(2)
,但它不起作用。收到电子邮件代理、Lotus Notes 后,google 邮件总是会创建新事件。
这是我创建和发送事件的代码:
try {
String from = "email@example.com";
String to = "email@example.com";
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.host", "smtp.example.com");
prop.setProperty("mail.user", "email");
prop.setProperty("mail.password", "");
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("Message Subject");
StringBuffer sb = new StringBuffer();
/*EVENT START*/
VEvent event = new VEvent();
event.setUid("ANATOLYTARNAVSKY123");
event.setDescription("Invitation Description");
event.setSummary("Invitation Description");
event.setOrganizer("anatolyt@gmail.com");
event.setLocation("room");
event.setSequence(sequence);
Calendar start = Calendar.getInstance();
start.add(Calendar.HOUR_OF_DAY, sequence + 2);
Calendar end = Calendar.getInstance();
end.add(Calendar.HOUR_OF_DAY, sequence + 2);
event.setDateStart(start.getTime());
event.setDateEnd(end.getTime());
icals.addEvent(event);
WriterChainText text = Biweekly.write(icals);
String result = text.go();
System.out.println(result);
/*EVENT END*/
StringBuffer buffer = sb.append(result);
// 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();
}
我将在此处放置用于发送邀请的代码的迭代:
提前致谢。
更新 1:
这是我的代码和 BiWeekly 库生成的第一个和第二个 VEVENT:
仅创建事件 - VEVENT 数据:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.4.3//EN
METHOD:REQUEST
BEGIN:VEVENT
DTSTAMP:20151113T100301Z
UID:ANATOLYTARNAVSKY123
DESCRIPTION:Invitation Description
SUMMARY:Invitation Description
ORGANIZER:mailto:anatolyt@example.com
LOCATION:room
SEQUENCE:0
DTSTART:20151113T120301Z
DTEND:20151113T120301Z
END:VEVENT
END:VCALENDAR
更新事件 - 仅 VEVENT 数据:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.4.3//EN
METHOD:REQUEST
BEGIN:VEVENT
DTSTAMP:20151113T100333Z
UID:ANATOLYTARNAVSKY123
DESCRIPTION:Invitation Description
SUMMARY:Invitation Description
ORGANIZER:mailto:anatolyt@example.com
LOCATION:room
SEQUENCE:1
DTSTART:20151113T130333Z
DTEND:20151113T130333Z
END:VEVENT
END:VCALENDAR
更新 2:
当我发送第一次和第二次邀请时,我在 gmail 上收到以下电子邮件:
完整的原始电子邮件内容,由于此处存在一些隐私问题,删除了 tracerout 路径:
创建事件 - 没有路由部分的完整电子邮件:
Date: Sat, 14 Nov 2015 08:03:47 -0700
From: anatolyt@example.com
To: anatolyt@gmail.com
Message-ID: *****
Subject: Escape Room Invitation
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_451111351.1447513426727"
X-TM-AS-MML: disable
X-Content-Scanned: Fidelis XPS MAILER
x-cbid: 15111415-0021-0000-0000-000004CDC424
method=REQUEST
charset=UTF-8
component=VEVENT
------=_Part_0_451111351.1447513426727
Content-Type: text/calendar; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Class: urn:content-classes:calendarmessage
Content-ID: calendar_message
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.4.3//EN
METHOD:REQUEST
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20151114T150346Z
UID:anatolyt@example.com
STATUS:CONFIRMED
DESCRIPTION:Invitation Description
SUMMARY:Invitation Summary
ORGANIZER:mailto:anatolyt@example.com
LOCATION:Escape room
TRANSP:OPAQUE
CREATED:20151114T150346Z
LAST-MODIFIED:20151114T150346Z
SEQUENCE:0
DTSTART:20151114T170346Z
DTEND:20151114T170346Z
END:VEVENT
END:VCALENDAR
------=_Part_0_451111351.1447513426727--
更新事件 - 没有路由部分的完整电子邮件:
Date: Sat, 14 Nov 2015 08:05:06 -0700
From: anatolyt@example.com
To: anatolyt@gmail.com
Message-ID: *****
Subject: Escape Room Invitation
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_451111351.1447513506015"
X-TM-AS-MML: disable
X-Content-Scanned: Fidelis XPS MAILER
x-cbid: 15111415-0029-0000-0000-000004D409DF
method=REQUEST
charset=UTF-8
component=VEVENT
------=_Part_0_451111351.1447513506015
Content-Type: text/calendar; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Class: urn:content-classes:calendarmessage
Content-ID: calendar_message
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.4.3//EN
METHOD:REQUEST
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20151114T150505Z
UID:anatolyt@example.com
STATUS:CONFIRMED
DESCRIPTION:Invitation Description
SUMMARY:Invitation Summary
ORGANIZER:mailto:anatolyt@example.com
LOCATION:Escape room
TRANSP:OPAQUE
CREATED:20151114T150505Z
LAST-MODIFIED:20151114T150505Z
SEQUENCE:1
DTSTART:20151114T180505Z
DTEND:20151114T180505Z
END:VEVENT
END:VCALENDAR
------=_Part_0_451111351.1447513506015--
更新 3
它看起来可以在 Lotus Notes 上正常工作,但 Google 日历仍然无法识别更新事件。
更新 4 - 解决方案
正如@arnaudq 所写,我错过了 ATTENDEE
属性 来制作 Google 日历来识别更新事件。这是一个如何做的例子:
VEvent event = new VEvent();
...
Attendee attendee = new Attendee("Anatoly Tarnavsky", "anatolyt@gmail.com");
attendee.setRsvp(true);
attendee.setRole(Role.CHAIR);
attendee.setParticipationStatus(ParticipationStatus.CONFIRMED);
...
event.setProperty(attendee);
您的代码似乎没有明确设置 UID 属性。因此,图书馆可能会为您生成一个新的。
UID 属性 用于识别此 VEVENT 等。
因此,您的更新应包含与之前发送的原始事件相同的 UID 值。
您还缺少至少一位与会者 属性。 ATTENDEE 之一应将其值设置为与接收邀请的 gmail 电子邮件地址相对应的 mailto uri。
尝试向 VEVENT 组件添加 LAST-MODIFIED 属性。您似乎在做其他所有事情——保持 UID 一致并递增 SEQUENCE。
我正在使用 BiWeekly library to create VEVENT and then send it by using Java Mail API。一切正常,但我如何为现有事件创建更新,即当我收到更新的 VEVENT 时,它不会在日历中创建新事件但会更新现有事件?
我试过按以下方式设置顺序 event.setSequence(2)
,但它不起作用。收到电子邮件代理、Lotus Notes 后,google 邮件总是会创建新事件。
这是我创建和发送事件的代码:
try {
String from = "email@example.com";
String to = "email@example.com";
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.host", "smtp.example.com");
prop.setProperty("mail.user", "email");
prop.setProperty("mail.password", "");
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("Message Subject");
StringBuffer sb = new StringBuffer();
/*EVENT START*/
VEvent event = new VEvent();
event.setUid("ANATOLYTARNAVSKY123");
event.setDescription("Invitation Description");
event.setSummary("Invitation Description");
event.setOrganizer("anatolyt@gmail.com");
event.setLocation("room");
event.setSequence(sequence);
Calendar start = Calendar.getInstance();
start.add(Calendar.HOUR_OF_DAY, sequence + 2);
Calendar end = Calendar.getInstance();
end.add(Calendar.HOUR_OF_DAY, sequence + 2);
event.setDateStart(start.getTime());
event.setDateEnd(end.getTime());
icals.addEvent(event);
WriterChainText text = Biweekly.write(icals);
String result = text.go();
System.out.println(result);
/*EVENT END*/
StringBuffer buffer = sb.append(result);
// 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();
}
我将在此处放置用于发送邀请的代码的迭代:
提前致谢。
更新 1:
这是我的代码和 BiWeekly 库生成的第一个和第二个 VEVENT:
仅创建事件 - VEVENT 数据:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.4.3//EN
METHOD:REQUEST
BEGIN:VEVENT
DTSTAMP:20151113T100301Z
UID:ANATOLYTARNAVSKY123
DESCRIPTION:Invitation Description
SUMMARY:Invitation Description
ORGANIZER:mailto:anatolyt@example.com
LOCATION:room
SEQUENCE:0
DTSTART:20151113T120301Z
DTEND:20151113T120301Z
END:VEVENT
END:VCALENDAR
更新事件 - 仅 VEVENT 数据:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.4.3//EN
METHOD:REQUEST
BEGIN:VEVENT
DTSTAMP:20151113T100333Z
UID:ANATOLYTARNAVSKY123
DESCRIPTION:Invitation Description
SUMMARY:Invitation Description
ORGANIZER:mailto:anatolyt@example.com
LOCATION:room
SEQUENCE:1
DTSTART:20151113T130333Z
DTEND:20151113T130333Z
END:VEVENT
END:VCALENDAR
更新 2:
当我发送第一次和第二次邀请时,我在 gmail 上收到以下电子邮件:
完整的原始电子邮件内容,由于此处存在一些隐私问题,删除了 tracerout 路径:
创建事件 - 没有路由部分的完整电子邮件:
Date: Sat, 14 Nov 2015 08:03:47 -0700
From: anatolyt@example.com
To: anatolyt@gmail.com
Message-ID: *****
Subject: Escape Room Invitation
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_451111351.1447513426727"
X-TM-AS-MML: disable
X-Content-Scanned: Fidelis XPS MAILER
x-cbid: 15111415-0021-0000-0000-000004CDC424
method=REQUEST
charset=UTF-8
component=VEVENT
------=_Part_0_451111351.1447513426727
Content-Type: text/calendar; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Class: urn:content-classes:calendarmessage
Content-ID: calendar_message
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.4.3//EN
METHOD:REQUEST
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20151114T150346Z
UID:anatolyt@example.com
STATUS:CONFIRMED
DESCRIPTION:Invitation Description
SUMMARY:Invitation Summary
ORGANIZER:mailto:anatolyt@example.com
LOCATION:Escape room
TRANSP:OPAQUE
CREATED:20151114T150346Z
LAST-MODIFIED:20151114T150346Z
SEQUENCE:0
DTSTART:20151114T170346Z
DTEND:20151114T170346Z
END:VEVENT
END:VCALENDAR
------=_Part_0_451111351.1447513426727--
更新事件 - 没有路由部分的完整电子邮件:
Date: Sat, 14 Nov 2015 08:05:06 -0700
From: anatolyt@example.com
To: anatolyt@gmail.com
Message-ID: *****
Subject: Escape Room Invitation
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_451111351.1447513506015"
X-TM-AS-MML: disable
X-Content-Scanned: Fidelis XPS MAILER
x-cbid: 15111415-0029-0000-0000-000004D409DF
method=REQUEST
charset=UTF-8
component=VEVENT
------=_Part_0_451111351.1447513506015
Content-Type: text/calendar; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Class: urn:content-classes:calendarmessage
Content-ID: calendar_message
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.4.3//EN
METHOD:REQUEST
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20151114T150505Z
UID:anatolyt@example.com
STATUS:CONFIRMED
DESCRIPTION:Invitation Description
SUMMARY:Invitation Summary
ORGANIZER:mailto:anatolyt@example.com
LOCATION:Escape room
TRANSP:OPAQUE
CREATED:20151114T150505Z
LAST-MODIFIED:20151114T150505Z
SEQUENCE:1
DTSTART:20151114T180505Z
DTEND:20151114T180505Z
END:VEVENT
END:VCALENDAR
------=_Part_0_451111351.1447513506015--
更新 3 它看起来可以在 Lotus Notes 上正常工作,但 Google 日历仍然无法识别更新事件。
更新 4 - 解决方案
正如@arnaudq 所写,我错过了 ATTENDEE
属性 来制作 Google 日历来识别更新事件。这是一个如何做的例子:
VEvent event = new VEvent();
...
Attendee attendee = new Attendee("Anatoly Tarnavsky", "anatolyt@gmail.com");
attendee.setRsvp(true);
attendee.setRole(Role.CHAIR);
attendee.setParticipationStatus(ParticipationStatus.CONFIRMED);
...
event.setProperty(attendee);
您的代码似乎没有明确设置 UID 属性。因此,图书馆可能会为您生成一个新的。
UID 属性 用于识别此 VEVENT 等。 因此,您的更新应包含与之前发送的原始事件相同的 UID 值。
您还缺少至少一位与会者 属性。 ATTENDEE 之一应将其值设置为与接收邀请的 gmail 电子邮件地址相对应的 mailto uri。
尝试向 VEVENT 组件添加 LAST-MODIFIED 属性。您似乎在做其他所有事情——保持 UID 一致并递增 SEQUENCE。