Send calendar invitations for each email using java
I'm trying to send calendar invitations for each email using Java The recipient receives an email, but the event does not show accepted or rejected invitations, but is automatically added to his calendar
I'm using ical4j Jar build activity / Invitation
private Calendar getInvite(Session session) { Calendar calendar = new Calendar(); calendar.getProperties().add(Version.VERSION_2_0); calendar.getProperties().add(Method.REQUEST); VEvent event = new VEvent( new DateTime(sesion.getStartDate()),new DateTime(sesion.getEndDate()),session.getName()); event.getProperties().add(Priority.MEDIUM); event.getProperties().add(Clazz.PUBLIC); try { UidGenerator ug = new UidGenerator("uidGen"); Uid uid = ug.generateUid(); event.getProperties().add(uid); } catch (SocketException e) { // Log things } for (Participant participant : session.getParticipants()) { Attendee attendee = new Attendee(URI.create("mailto:" + participant.getEmail())); attendee.getParameters().add(Role.OPT_PARTICIPANT); attendee.getParameters().add(new Cn(participant.getName())); attendee.getParameters().add(PartStat.NEEDS_ACTION); event.getProperties().add(attendee); } calendar.getComponents().add(event); return calendar; }
This is how I send emails:
public void sendEmail(String fromMail,String toMail,String subject,String text,net.fortuna.ical4j.model.Calendar calendar) { try { Session session = Session.getInstance(getMailProperties(),new javax.mail.Authenticator() { protected PasswordAuthentication getpasswordAuthentication() { return new PasswordAuthentication(getUser(),getpassword()); } }); MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setHeader("Content-@R_594_301@:","7bit"); Address address = new InternetAddress(fromMail); mimeMessage.setFrom(address); mimeMessage.setSentDate(Calendar.getInstance().getTime()); mimeMessage.setRecipients(Message.RecipientType.TO,toMail); mimeMessage.setSubject(subject); Calendar cal = Calendar.getInstance(); mimeMessage.setSentDate(cal.getTime()); Multipart multipart = new MimeMultipart("alternative"); // First part - HTML readable text MimeBodyPart msgHtml = new MimeBodyPart(); msgHtml.setContent(text,"text/html; charset=UTF-8"); multipart.addBodyPart(msgHtml); if (calendar != null) { // Another part for the calendar invite MimeBodyPart invite = new MimeBodyPart(); invite.setHeader("Content-Class","urn:content- classes:calendarmessage"); invite.setHeader("Content-ID","calendar_message"); invite.setHeader("Content-Disposition","inline"); invite.setContent(calendar.toString(),"text/calendar"); multipart.addBodyPart(invite); } mimeMessage.setContent(multipart); Transport.send(mimeMessage); } catch (Exception e) { // Log things } }
But when I receive an email (in Gmail), I can't see the invitation, and the event will be automatically added to my calendar I can only accept or reject by clicking on an event in my calendar
I tried to send an invitation, and then what happened was that I received an email with an ICS attachment
What did I miss?
Solution
You are creating a new calendar, which is why calendars are automatically added Refer to the documentation https://github.com/ical4j/ical4j/wiki/Examples#Creating_a_new_calendar Try "create a four hour meeting" and see if there are still problems