0

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:baptiste.arnaud95@gmail.com\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

Baptiste Arnaud
  • 2,522
  • 3
  • 25
  • 55

1 Answers1

1

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.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • I did what you told me here, but it didn't fix the problem (my question is edited if you want to take a look again). Does the problem can be related to the account used to send emails? Because I use the gmail smtp server. Maybe it's related to the problem. – Baptiste Arnaud Nov 19 '18 at 10:01
  • The code looks better. Compare the MIME content of the message that works correctly with the MIME content of the message you're creating. You can see the MIME content of the message you're sending in the [JavaMail debug output](https://javaee.github.io/javamail/FAQ#debug). – Bill Shannon Nov 19 '18 at 17:53
  • BTW, in eventIcs, the explicit use of StringBuffer isn't helping at all. You might as well just construct a String object using concatenation ("+"). As it is, you're constructing the String and then appending that one String to the StringBuffer. – Bill Shannon Nov 19 '18 at 17:56
  • I found the solution, a REQUEST header was missing : `messageBodyEvent.dataHandler = ByteArrayDataSource(createEvent(), "text/calendar; method=REQUEST")`. It totally worked with gmail smtp server. But now I'm using my company's credentials logged in an outlook server and It throws a weird error :`STOREDRV.Submission.Exception:InvalidRecipientsException; Failed to process message due to a permanent exception with message A message can't be sent because it contains no recipients.`... Whenever I remove the REQUEST header it sends the mail correctly. What's wrong ? – Baptiste Arnaud Nov 20 '18 at 16:16
  • It would be better to post a new question for the new problem. The [JavaMail debug output](https://javaee.github.io/javamail/FAQ#debug) might help us figure out what's wrong. – Bill Shannon Nov 20 '18 at 21:04
  • Thanks Bill, I created a new complete question [here](https://stackoverflow.com/questions/53408299/send-ics-file-rsvp-format-not-working-properly-using-outlook-smtp-server). – Baptiste Arnaud Nov 21 '18 at 13:23