I’m trying to send a calendar invite using a Java service. The email is sent appropriately, however, the calendar invite is received as an attachment in the form of a .ics file. However, rather than having to click on the file to view the invite information, I’d like to send it as a meeting invitation that includes accept/reject options.
Below is my code; I am unable to find my mistake. Please assist me!
public String sendCalendarInvite(MailDetails mailDetails) {
try {
JavaMailSenderImpl mailSender = setMailConfig();
MimeMessage mimeMessage = mailSender.createMimeMessage();
String toEmail = mailDetails.getToEmail();
mimeMessage.setRecipients(MimeMessage.RecipientType.TO, toAddresses.toArray(new InternetAddress[0]));
String subject = mailDetails.getSubject();
String from = mailDetails.getFromEmail();
String startTime = mailDetails.getStartTime();
String endTime = mailDetails.getEndTime();
String mailBody = mailDetails.getMailBody();
String contentType = mailDetails.getContentType();
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.setSubject(subject);
mimeMessage.addHeaderLine("method=REQUEST");
mimeMessage.addHeaderLine("component=VEVENT");
StringBuilder buffer = new StringBuilder();
buffer.append("BEGIN:VCALENDARn" +
"METHOD:REQUESTn" +
"PRODID:Microsoft Exchange Server 2010n" +
"VERSION:2.0n" +
"BEGIN:VTIMEZONEn" +
"TZID:Asia/Kolkatan" +
"END:VTIMEZONEn" +
"BEGIN:VEVENTn" +
"ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:" + toEmail + "n" +
"ORGANIZER;CN=Foo:MAILTO:" + from + "n" +
"DESCRIPTION;LANGUAGE=en-US:" + mailBody + "n" +
"UID:"+UUID.randomUUID()+"n" +
"SUMMARY;LANGUAGE=en-US:Discussionn" +
"DTSTART:" + startTime + "n" +
"DTEND:" + endTime + "n" +
"CLASS:PUBLICn" +
"PRIORITY:5n" +
"DTSTAMP:20200922T105302Zn" +
"TRANSP:OPAQUEn" +
"STATUS:CONFIRMEDn" +
"SEQUENCE:$sequenceNumbern" +
"LOCATION;LANGUAGE=en-US:Microsoft Teams Meetingn" +
"BEGIN:VALARMn" +
"DESCRIPTION:REMINDERn" +
"TRIGGER;RELATED=START:-PT15Mn" +
"ACTION:DISPLAYn" +
"END:VALARMn" +
"END:VEVENTn" +
"END:VCALENDAR");
// Create the message part
MimeBodyPart calendarEventBodyPart = new MimeBodyPart();
// Create first body part for calendar event
calendarEventBodyPart.setHeader("Content-Class", "urn:content-classes:calendarmessage");
calendarEventBodyPart.setHeader("Content-ID", "calendar_message");
calendarEventBodyPart.setDataHandler(new DataHandler(
new ByteArrayDataSource( buffer.toString(), "text/calendar;method=REQUEST")));
// Create a Multipart
MimeMultipart multipart = new MimeMultipart("alternative");
multipart.addBodyPart(calendarEventBodyPart);
mimeMessage.setContent(multipart);
mimeMessage.setReplyTo(new InternetAddress[]{new InternetAddress(Q_DL)});
mimeMessage.setSentDate(new Date());
mimeMessage.saveChanges();
logger.info("Sending mail ");
logger.info("mail id: {}", mimeMessage.getMessageID());
mailSender.send(mimeMessage);
logger.info("Calendar invite sent successfully");
return "Calendar invite Sent Successfully.";
} catch (AddressException e) {
throw new CustomRuntimeException("Address Exception during sending mail :" + e.getMessage());
} catch (MessagingException e) {
throw new CustomRuntimeException("Messaging Exception during sending mail: " + e.getMessage());
} catch (Exception e) {
throw new CustomRuntimeException("Exception during sending mail: " + e.getMessage());
}
}
I am trying to send a outlook calendar invite but it is received as an attachment.
sneha agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.