Java 改型2通过MS graph api发送邮件返回参数值';消息';是空的
我有一个JavaSpring项目,它使用Reformation2对来自Microsoft的GraphAPI进行API调用。我能够验证、阅读邮箱和检索/阅读电子邮件。然而,我正在努力发送邮件 我有以下发送电子邮件的代码: 权限:包含邮件。发送Java 改型2通过MS graph api发送邮件返回参数值';消息';是空的,java,spring,retrofit2,microsoft-graph-api,outlook-restapi,Java,Spring,Retrofit2,Microsoft Graph Api,Outlook Restapi,我有一个JavaSpring项目,它使用Reformation2对来自Microsoft的GraphAPI进行API调用。我能够验证、阅读邮箱和检索/阅读电子邮件。然而,我正在努力发送邮件 我有以下发送电子邮件的代码: 权限:包含邮件。发送 private String[] scopes = [ "openid", "email", "profile", "User.Read",
private String[] scopes = [
"openid",
"email",
"profile",
"User.Read",
"Mail.Read",
"Mail.Send",
"Mail.ReadWrite",
"Calendars.Read",
"Calendars.ReadWrite",
"Contacts.Read",
"Contacts.ReadWrite",
"offline_access"
];
改装电话:
@Headers("Content-Type: application/json")
@POST("/v1.0/me/sendMail")
Call<Message> sendMessage(
@Body String messageObject
);
"{\"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\"}"
<-- 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)
HTTP响应:
@Headers("Content-Type: application/json")
@POST("/v1.0/me/sendMail")
Call<Message> sendMessage(
@Body String messageObject
);
"{\"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\"}"
<-- 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)
我认为,在实际发送json数据时,不应该使用双引号的转义字符
因此,发出的字符串应该是不可替换的,并且看起来像这样
{"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"}
此外,你会发现
地址
不是收件人
的有效属性,您应该在电子邮件地址
属性中
name
属性无效,应删除
ContentType
值可以是text
或html
因此,发出的json应该是这样的
{
"Message": {
"toRecipients": [
{
"emailAddress": {
address:"test@test.be"
}
}
],
"Body": {
"ContentType": "html",
"Content": "value(Email_body)=%3Cp%3Etesttext%3C%2Fp%3E"
},
"Subject": "testsubject"
},
"SaveToSentItems": "true"
}
您还可以通过以下链接阅读有关sendMail api的更多信息
所以我想,通过改型2发布原始json的正确方法是:
@Headers("Content-Type: application/json")
@POST("/v1.0/me/sendMail")
Call<Message> sendMessage(
@Body HashMap<String, Object> messageObject
);
@Headers(“内容类型:application/json”)
@POST(“/v1.0/me/sendMail”)
呼叫发送消息(
@Body HashMap messageObject
);
我在MS链接中尝试了该示例,但它返回了相同的错误。可能是改型调用出错了。您是否使用类似Gson的库构造json字符串对象?或者您只是将一个字符串作为参数传递给改造调用吗?我使用gson构建了json,但似乎改造2需要一个HashMap
来将其发布为ure jsonHappy,很高兴听到您对其进行了整理。:)