emailmailjet

How would you attach an excel document using MailJet api


Hello I have tried using mailjets api to send an excel document. I first encode the excel file to base64 using mailjets library. I then use mailjets library to add the file as an attachment and set the mime type application/vnd.ms-excel. Heres an example.

 request = new MailjetRequest(Emailv31.resource)
            .property(Emailv31.MESSAGES, new JSONArray()
                .put(new JSONObject()
                    .put(Emailv31.Message.FROM, new JSONObject()
                        .put("Email", "pilot@mailjet.com")
                        .put("Name", "Mailjet Pilot"))
                    .put(Emailv31.Message.TO, new JSONArray()
                        .put(new JSONObject()
                            .put("Email", "passenger1@mailjet.com")
                            .put("Name", "passenger 1")))
                    .put(Emailv31.Message.SUBJECT, "Your email flight plan!")
                    .put(Emailv31.Message.TEXTPART, "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!")
                    .put(Emailv31.Message.HTMLPART, "<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!")
                    .put(Emailv31.Message.ATTACHMENTS, new JSONArray()
                        .put(new JSONObject()
                            .put("ContentType", "application/vnd.ms-excel")
                            .put("Filename", "test.txt")
                            .put("Base64Content", "excelFileBase64")))));
      response = client.post(request);

However I always receive the file as its string represented base64 encoded.


Solution

  • Use ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

    and test.xlsx