Retrofit 2 send mail via MS graph api returns The value of the parameter 'Message' is empty

Viewed 774

I have a Java Spring project which uses Retrofit 2 to make API calls to the Graph API from Microsoft. I am able to authenticate, read mail boxes and retrieve/read emails. However i am struggling with sending mails.

I have the following code for sending an email:

Permissions: Contains the Mail.send

    private String[] scopes = [
            "openid",
            "email",
            "profile",
            "User.Read",
            "Mail.Read",
            "Mail.Send",
            "Mail.ReadWrite",
            "Calendars.Read",
            "Calendars.ReadWrite",
            "Contacts.Read",
            "Contacts.ReadWrite",
            "offline_access"
    ];

Retrofit Call:

    @Headers("Content-Type: application/json")
    @POST("/v1.0/me/sendMail")
    Call<Message> sendMessage(
            @Body String messageObject
    );

The String that is sent is a JSONString:

"{\"Message\":{\"toRecipients\":[{\"address\":\"test@test.be\",\"name\":\"test@test.be\"}],\"Body\":{\"ContentType\":\"text/html\",\"Content\":\"value(Email_body)=%3Cp%3Etesttext%3C%2Fp%3E\"},\"Subject\":\"testsubject\"},\"SaveToSentItems\":\"true\"}"

HTTP Response:

 <-- 400 Bad Request https://graph.microsoft.com/v1.0/me/sendMail (101ms)
 Cache-Control: private
 Transfer-Encoding: chunked
 Content-Type: application/json
 request-id: 94d19c39-4b84-4ee9-98d2-d42d36886e40
 client-request-id: 0c9d2c63-28bf-49c4-9ad6-08d81c6fd4b0
 x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"West Europe","Slice":"SliceC","Ring":"5","ScaleUnit":"003","RoleInstance":"AGSFE_IN_27"}}
 Strict-Transport-Security: max-age=31536000
 Date: Wed, 09 Oct 2019 07:41:54 GMT

 {
  "error": {
    "code": "ErrorInvalidParameter",
    "message": "The value of the parameter 'Message' is empty.",
    "innerError": {
      "request-id": "94d19c39-4b84-4ee9-98d2-d42d36886e40",
      "date": "2019-10-09T07:41:54"
    }
  }
}
<-- END HTTP (253-byte body)

Any idea what is causing the error?

2 Answers

I believe that you should not be having the escaping characters for the double quotes when the json data is actually being sent out.

Therefore the string sent out should be unescaped and look something like this.

{"Message":{"toRecipients":[{"address":"test@test.be","name":"test@test.be"}],"Body":{"ContentType":"text/html","Content":"value(Email_body)=%3Cp%3Etesttext%3C%2Fp%3E"},"Subject":"testsubject"},"SaveToSentItems":"true"}

Furthermore, you will discover that

  • address is not a valid property for toRecipientsand you should be inside an emailAddress property
  • name property is invalid and should be removed.
  • ContentType values can be text or html

Therefore, the json sent out should look something like this.

{
  "Message": {
    "toRecipients": [
      {
        "emailAddress": {
            address:"test@test.be"
        }
      }
    ],
    "Body": {
      "ContentType": "html",
      "Content": "value(Email_body)=%3Cp%3Etesttext%3C%2Fp%3E"
    },
    "Subject": "testsubject"
  },
  "SaveToSentItems": "true"
}

You can also read more about the sendMail api at the following link

https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http

So i figured it out, the correct way to post raw json with retrofit 2 is:

    @Headers("Content-Type: application/json")
    @POST("/v1.0/me/sendMail")
    Call<Message> sendMessage(
            @Body HashMap<String, Object> messageObject
    );
Related