Search code examples
javaspring-bootoutlookjakarta-mailicalendar

Send ICS file via SMTP not correctly displayed in Outlook?


I'm trying to send calendar invites from my backend server.

Here is the function involved :

fun sendEventInvite(to: String, subject: String, text: String) {
    val message: MimeMessage = emailSender.createMimeMessage()

    message.setRecipient(Message.RecipientType.TO, InternetAddress(to))
    message.subject = subject

    val messageBodyText = MimeBodyPart()
    messageBodyText.setText(text)
    val messageBodyEvent = MimeBodyPart()
    messageBodyEvent.dataHandler = ByteArrayDataSource(createEvent(), "text/calendar")

    val multiPart = MimeMultipart()
    multiPart.addBodyPart(messageBodyEvent)

    message.setContent(multiPart)

    emailSender.send(message)
}

And here is how I format the ICS file:

fun createEvent(): String{
        return "BEGIN:VCALENDAR\n" +
        "VERSION:2.0\n" +
        "PRODID:-//GRTgaz Corporation//NONSGML Togaz'er//FR\n" +
        "METHOD:REQUEST\n" +
        "BEGIN:VEVENT\n" +
        "UID:d8f5a0777-bf6d-25d2-f14a-52e7fe3df810\n" +
        "DTSTAMP:20181119T105044Z\n" +
        "ORGANIZER;CN=Baptiste Arnaud:MAILTO:[email protected]\n" +
        "DTSTART:20181120T150000\n" +
        "DTEND:20181120T153000\n" +
        "SUMMARY:Description\n" +
        "END:VEVENT\n" +
        "END:VCALENDAR\n")
    }

This file content is supposed to work because it is exactly the same of a working example. So the problem comes from mail headers ? But I'm not sure what's missing.

How it should works:

enter image description here

But it's displayed like this:

enter image description here


Solution

  • All of the calls to addHeaderLine are not valid MIME headers so I don't know what you're trying to accomplish with that.

    The use of MimeHelper is just confusing things. Call the corresponding methods on the MimeMessage object directly, and add "text" as the first MimeBodyPart in the multipart, before the ics attachment.