I am using Sendgrid for sending emails and Ical4j library to create a calendar invite. The below-mentioned code was working fine a few months back and all the calendar invites sent through were also showing up all the required things such as; Accept/Decline button, start and end date etc., once received over any email client (mainly Outlook and Gmail). But now I'm able to send calendar invites through this code but once it is received over Outlook email client (It is working fine over Gmail); it is showing up as an (.ics) attachment instead of all the required calendar invite controls.
I did try several approaches but none worked.
Links used for reference
- Send email as calendar invite/appointment in SendGrid C#
- Calendar invite is received as ICS file in outlook - Laravel
- https://github.com/ical4j/ical4j/issues/236
Below is the java code to send calendar invite using sendgrid api
public Response sendCalendarInvite(Show show, List<String> tos, String cc, String subject, String body,
Calendar calendarEvent) {
Response response = new Response();
log.debug(
"Sending Calendar Invite tos : {}, CC: {}, Subject : {}, Body : {}, calendarEvent : {}",
tos, cc, subject, body, calendarEvent);
if (!ApplicationConfig.isEmailEnabled()) {
log.warn("Email not enabled. Check property [email.enabled] in application.properties");
return response;
}
try {
CalendarOutputter calendarOutputter = new CalendarOutputter();
Writer wtr = new StringWriter();
calendarOutputter.setValidating(true);
calendarOutputter.output(calendarEvent, wtr);
StringBuffer buffer = ((StringWriter) wtr).getBuffer();
Mail mail = new Mail();
mail.setFrom(
new Email("testfrom@test.com", "test name"));
mail.setSubject(subject);
Content content = new Content("text/calendar", String.valueOf(buffer).intern());
mail.addContent(content);
Personalization personalization = new Personalization();
personalization.addHeader("charset", "utf-8");
personalization.addHeader("method", calendarEvent.getMethod().getValue());
for (String to : tos) {
personalization.addTo(new Email(to));
}
mail.addPersonalization(personalization);
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint(END_POINT);
request.setBody(mail.build());
response = sg.api(request);
log.debug("{}, {}, {}", response.getStatusCode(), response.getBody(), response.getHeaders());
} catch (IOException ex) {
log.error("Calendar invite Exception", ex);
response.setBody(ex.getMessage());
throw new CustomException(CustomError.INTERNAL_SERVER_ERROR,
"Error occurred while sending Calendar invite");
} finally {
return response;
}
}
}
The calendar invite that I am trying to send
BEGIN:VCALENDAR
PRODID:-//XYZ//iCal4j 2.1//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VTIMEZONE
TZID:Etc/GMT
TZURL:http://tzurl.org/zoneinfo/Etc/GMT
X-LIC-LOCATION:Etc/GMT
BEGIN:STANDARD
TZOFFSETFROM:+0000
TZOFFSETTO:+0000
TZNAME:GMT
DTSTART:16010101T000000
RDATE:16010101T000000
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTAMP:20180820T091756Z
TZID:Etc/GMT
STATUS:CONFIRMED
ORGANIZER;CN=XXXX:mailto:donotreply@xxxx.com
UID:00803a42-e17d-41a9-b552-036f14c7799e
DTSTART:20180820T153000Z
DTEND:20180820T163000Z
LOCATION:Test Subject
SUMMARY:Test Summary
DESCRIPTION:Test Desc
LAST-MODIFIED:20180820T091756Z
SEQUENCE:0
TRANSP:OPAQUE
CLASS:PUBLIC
PRIORITY:5
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=
TRUE;X-NUM-GUESTS=0;CN=XYZ:mailto:xxxx.xxxx@yyyy.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=
TRUE;X-NUM-GUESTS=0;CN=XYZ:mailto:xxxxxxxx@gmail.com
END:VEVENT
END:VCALENDAR