您好,我最近使用 mailgun api 发送邮件。 我可以发送邮件,但我需要知道我的每封邮件是否已发送。
所以我搜索提供送达报告的 mailgun api。我找到了事件 api。
我阅读了所有文档,但无法像文档中提到的那样得到正确的回复。
我的代码工作正常,但无法获得响应。
这是我的代码。
public static ClientResponse GetLogs() {
Client client = new Client();
client.addFilter(new HTTPBasicAuthFilter("api","YOUR_API_KEY"));
WebResource webResource =client.resource("https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/events");
MultivaluedMapImpl queryParams = new MultivaluedMapImpl();
queryParams.add("event", "rejected OR failed");
return webResource.queryParams(queryParams).get(ClientResponse.class);
}
我收到了回复
GET https://api.mailgun.net/v3/sandboxcf4a9eb67b97489ab540dcc0e865cb0d.mailgun.org/events?event=delivered returned a response status of 200 OK
根据 mailgun 的文档应该是这样的
{
"items": [
{
"severity": "temporary",
"tags": [],
"envelope": {
"sender": "me@samples.mailgun.org",
"transport": ""
},
.
.
.
.
}
我在网上搜索解决方案,但找不到合适的解决方案。
请帮忙解决这个问题。
提前致谢。
最佳答案
是的,我得到了解决方案,实际上我在控制台中打印响应,如果在 java 中打印对象,它会调用字符串方法。这就是为什么我的输出是
ET https://api.mailgun.net/v3/sandboxcf4a9eb67b97489ab540dcc0e865cb0d.mailgun.org/events?event=delivered returned a response status of 200 OK
我应该读取输入流并将其解析为 json 对象。
现在我从 mailgun 得到了预期的响应。
我知道在 java 中使用 json web 服务,这就是我遇到麻烦的原因。
解析响应的代码是....
JSONObject jsonObject = null;
InputStream inputSrem = clientResponse.getEntityInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputSrem, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
jsonObject = new JSONObject(responseStrBuilder.toString());
希望对其他人也有帮助。
关于java - 无法读取 mailgun api 事件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35620589/