Send ics file RSVP format not working properly usi

2019-07-12 22:48发布

问题:

I have a code that works well when using gmail smtp server with my personal credentials :

fun sendEventInvitation(from: Profile, to: Profile, meetup: Meetup) {
    val message: MimeMessage = emailSender.createMimeMessage()

    message.setFrom(InternetAddress("foo1@foo1.com"))
    message.setRecipient(Message.RecipientType.TO, InternetAddress("foo2@foo2.com"))
    message.subject = "foo"

    val messageBodyEvent = MimeBodyPart()
    val id = UUID.randomUUID().toString()
    val eventIcs = formatCalendarFile(id, from, to, meetup)
    messageBodyEvent.dataHandler = DataHandler(ByteArrayDataSource(eventIcs, "text/calendar; charset=\"utf-8\"; method=REQUEST"))
    messageBodyEvent.setHeader("Content-Transfer-Encoding", "base64")

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

    message.setContent(multiPart)

    emailSender.send(message)
}

eventIcs is basically a String of an iCal correct format.

But whenever I switch to the outlook server using company's credentials, It gives the following error :

554 5.2.0 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. InvalidRecipientsException: A message can't be sent because it contains no recipients. [Hostname=AM6PR01MB5011.eurprd01.prod.exchangelabs.com]
DEBUG SMTP: got response code 554, with response: 554 5.2.0 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. InvalidRecipientsException: A message can't be sent because it contains no recipients. [Hostname=AM6PR01MB5011.eurprd01.prod.exchangelabs.com]

Now, weirdly enough, when I remove the method=REQUEST attachment header :

messageBodyEvent.dataHandler = DataHandler(ByteArrayDataSource(eventIcs, "text/calendar; charset=\"utf-8\"))

It works but because there's not this header, the ICS isn't well recognized by Outlook client and doesn't display RSVP options to the recipients as explained here.

I tried to send an invitation from Thunderbird using the same credentials and it works. So it's not link to rules whatsoever.

Here is the complete debug output :

DEBUG: JavaMail version 1.6.1
DEBUG: URL jar:file:/Users/baptistearnaud/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.11.373/aws-java-sdk-ses-1.11.373.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/Users/baptistearnaud/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.11.373/aws-java-sdk-ses-1.11.373.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.amazonaws.services.simpleemail.AWSJavaMailTransport=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], aws=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
220 LO2P265CA0439.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 21 Nov 2018 11:01:22 +0000
DEBUG SMTP: connected to host "smtp.office365.com", port: 587
EHLO mbp-de-baptiste.lan
250-LO2P265CA0439.outlook.office365.com Hello [89.227.204.114]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "157286400"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO mbp-de-baptiste.lan
250-LO2P265CA0439.outlook.office365.com Hello [89.227.204.114]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-AUTH LOGIN XOAUTH2
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "157286400"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN XOAUTH2"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: protocolConnect login, host=smtp.office365.com, user=SVC_ToGazer@uccint.onmicrosoft.com, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<foo1@foo1.com>
250 2.1.0 Sender OK
RCPT TO:<foo2@foo2.com>
250 2.1.5 Recipient OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   foo2@foo2.com
DATA
354 Start mail input; end with <CRLF>.<CRLF>
Date: Wed, 21 Nov 2018 11:00:41 +0000 (UTC)
From: foo1@foo1.com
To: foo2@foo2.com
Message-ID: <681000169.0.1542798041668@mbp-de-baptiste.lan>
Subject: FOO
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_1_1921144901.1542798041673"

------=_Part_1_1921144901.1542798041673
Content-Type: text/calendar; charset="utf-8"; method=REQUEST
Content-Transfer-Encoding: base64

QkVHSU46VkNBTEVOREFSClZFUlNJT046Mi4wClBST0RJRDotLy9HUlRnYXogQ29ycG9yYXRpb24v
L05PTlNHTUwgVG9nYXonZXIvL0ZSCk1FVEhPRDpSRVFVRVNUCkJFR0lOOlZFVkVOVApVSUQ6YTQ5
ODVkZjUtNzI1YS00NTA0LTgzNWYtZDhkNTU2ZjliMDIzQHRvZ2F6ZXIuY29tCkRUU1RBTVA6MjAx
ODExMjlUMTgwMDAwWgpPUkdBTklaRVI7Q049dG9nYXplckBpbnQuZ3J0Z2F6LmV1Om1haWx0bzp0
b2dhemVyQGludC5ncnRnYXouZXUKQVRURU5ERUU7Q1VUWVBFPUlORElWSURVQUw7Uk9MRT1SRVEt
UEFSVElDSVBBTlQ7ClBBUlRTVEFUPU5FRURTLUFDVElPTjtSU1ZQPVRSVUU7Q049QmFwdGlzdGUg
QXJuYXVkOwpYLU5VTS1HVUVTVFM9MDptYWlsdG86YmFwdF9pczJAaG90bWFpbC5jb20KRFRTVEFS
VDoyMDE4MTEyOVQxODAwMDAKRFRFTkQ6MjAxODExMjlUMjIwMDAwClNVTU1BUlk6QWZ0ZXJ3b3Jr
IGdheidwYWNobwpMT0NBVElPTjpQYXJpcyA4ZSBhcnIKREVTQ1JJUFRJT046QWZ0ZXJ3b3JrIG/D
uSBsJ29uIHZhIGTDqWd1c3RlciBkdSBib24gZ2FzcGFjaG8uIEonZXNww6hyZSBxdWUgbGUgamV1
IGRlIG1vdCB2b3VzIHBsYcOudC4uLiBFeGNlbGxlbnQhCkVORDpWRVZFTlQKRU5EOlZDQUxFTkRB
Ugo=
------=_Part_1_1921144901.1542798041673--
.
554 5.2.0 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. InvalidRecipientsException: A message can't be sent because it contains no recipients. [Hostname=AM6PR01MB5011.eurprd01.prod.exchangelabs.com]
DEBUG SMTP: got response code 554, with response: 554 5.2.0 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. InvalidRecipientsException: A message can't be sent because it contains no recipients. [Hostname=AM6PR01MB5011.eurprd01.prod.exchangelabs.com]

RSET
250 2.0.0 Resetting
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 554 5.2.0 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. InvalidRecipientsException: A message can't be sent because it contains no recipients. [Hostname=AM6PR01MB5011.eurprd01.prod.exchangelabs.com]

Any ideas why there is this behavior ?

回答1:

As mentioned @Bill Shannon in the comment section, the problem came from the calendar formatting.

You need to make sure your ics pass this kind of validator.

For example, in my implementation :

"""BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//GRTgaz Corporation//NONSGML Togaz'er//FR
METHOD:REQUEST
BEGIN:VEVENT
UID:$id
ORGANIZER;RSVP=FALSE;PARTSTAT=ACCEPTED;
ROLE=CHAIR:mailto:SVC_ToGazer@uccint.onmicrosoft.com
ATTENDEE;RSVP=FALSE;PARTSTAT=ACCEPTED;
ROLE=REQ-PARTICIPANT:mailto:${to.email}
DTSTAMP:${startDate[0]}T${startDate[1]}Z
DTSTART:${startDate[0]}T${startDate[1]}
DTEND:${endDate[0]}T${endDate[1]}
SUMMARY:${meetup.name}
LOCATION:${meetup.address}
DESCRIPTION:${meetup.description}
END:VEVENT
END:VCALENDAR
"""

The ORGANIZER and ATTENDEE props weren't valid. the property ROLE must be in the same line. So I modified it to:

"""BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//GRTgaz Corporation//NONSGML Togaz'er//FR
METHOD:REQUEST
BEGIN:VEVENT
UID:$id
ORGANIZER;RSVP=FALSE;PARTSTAT=ACCEPTED;ROLE=CHAIR
 :mailto:SVC_ToGazer@uccint.onmicrosoft.com
ATTENDEE;RSVP=FALSE;PARTSTAT=ACCEPTED;ROLE=REQ-PARTICIPANT
 :mailto:${to.email}
DTSTAMP:${startDate[0]}T${startDate[1]}Z
DTSTART:${startDate[0]}T${startDate[1]}
DTEND:${endDate[0]}T${endDate[1]}
SUMMARY:${meetup.name}
LOCATION:${meetup.address}
DESCRIPTION:${meetup.description}
END:VEVENT
END:VCALENDAR
"""

Now, it works properly.