MailJet无法从Java电子邮件发送:(400 Bad Request)

huangapple go评论74阅读模式
英文:

MailJet Not able to send from java email: ( 400 Bad Request )

问题

我尝试从我们的Java项目中发送电子邮件,但每次都出现400错误代码。所以我从Java控制台中获取相同的请求并在Postman中发送,使用相同的其他元素,然后从Postman中可以正常工作,我可以收到邮件,但从Java中仍然出现400错误的坏请求,当我打开Mailjet调试模式时,内容为空。

这是Java生成并从Postman注入的最终JSON:

{
	"Messages": [
		{
			"Variables": {
				"objet": " objet",
				"body": " body "
			},
			"From": {
				"Email": "no-reply@gmail.com"
			},
			"To": [
				{
					"Email": "send@gmail.com",
					"Name": ""
				}
			],
			"TemplateID": 111111,
			"TemplateLanguage": true,
			"Subject": "[[data:objet:\" anything\"]]"
		}
	]
}

Java代码:

/**
 * 发送消息
 * @return MailjetResponse
 * @throws MailjetException
 * @throws MailjetSocketTimeoutException
 */
public MailjetRequest sendEmailCore()  {
    return new MailjetRequest(Emailv31.resource)
            .property(Emailv31.MESSAGES, new JSONArray()
                    .put(new JSONObject()
                            .put(Emailv31.Message.FROM, new JSONObject()
                                .put("Email", senderMail)
                                .put("Name", senderName))
                            .put(Emailv31.Message.TO, getRecipientsJson())
                            .put(Emailv31.Message.TEMPLATEID, TEMPLATE_ID)
                            .put(Emailv31.Message.TEMPLATELANGUAGE, true)
                            .put(Emailv31.Message.SUBJECT, "[[data:objet:\" anything\"]]")
                            .put(Emailv31.Message.VARIABLES, new JSONObject()
                                .put("objet", " objet")
                                .put("mailTitle", mailTitle)
                                .put("body", body))));
}

private JSONArray getRecipientsJson(){
    JSONArray json = new JSONArray();
    for (String recipient : recipients) {
        json.put(new JSONObject()
                    .put("Email", recipient)
                    .put("Name", ""));
    }
    return json;
}

public MailjetResponse sendLoggedMail() throws Exception {
    MailjetClient client = new MailjetClient(API_KEY_PUBLIC, API_KEY_PRIVATE, new ClientOptions("v3.1"));
    MailjetRequest request = sendEmailCore();
    MailjetResponse response = null;
    try {
        client.setDebug(MailjetClient.VERBOSE_DEBUG);
        log.info(request.getBody());
        response = client.post(request);
    }  catch (final MailjetException | MailjetSocketTimeoutException e) {
    
        log.error("sendLoggedMail", e);
    } 
    return response;
}

MailJet日志:

=== HTTP Request ===
POST https://api.mailjet.com/v3.1/send
Accept-Charset: UTF-8
Accept: application/json
user-agent: mailjet-apiv3-java/v4.5.0
Content-Type: application/json
=== HTTP Response ===
Receive url: https://api.mailjet.com/v3.1/send
Status: 400
date: Tue, 01 Sep 2020 08:59:55 GMT
null: HTTP/1.1 400 Bad Request
content-length: 177
x-mj-request-guid: 0bb2a0ac-849d-4d80-82ed-a10a792e8d19
content-type: application/json; charset=UTF-8
Content:
null

感谢您的帮助。

英文:

I try to send an email from our java project but every time I have a code error 400. So I take the same request printed in the java console and send it from Postman with the same other elements then it works from Postman and i receive the email but from java I have always the code error 400 bad request and when i turn on the Mailjet debug mode the content is null.

Here the final JSON made by Java and inject from Postman

{
"Messages": [
{
"Variables": {
"objet": " objet",
"body": " body "
},
"From": {
"Email": "no-reply@gmail.com"
},
"To": [
{
"Email": "send@gmail.com",
"Name": ""
}
],
"TemplateID": 111111,
"TemplateLanguage": true,
"Subject": "[[data:objet:\" anything\"]]"
}
]
}

The java code :

/**
*  Send a message
* @return MailjetResponse
* @throws MailjetException
* @throws MailjetSocketTimeoutException
*/
public MailjetRequest sendEmailCore()  {
return new MailjetRequest(Emailv31.resource)
.property(Emailv31.MESSAGES, new JSONArray()
.put(new JSONObject()
.put(Emailv31.Message.FROM, new JSONObject()
.put("Email", senderMail)
.put("Name", senderName))
.put(Emailv31.Message.TO, getRecipientsJson())
.put(Emailv31.Message.TEMPLATEID, TEMPLATE_ID)
.put(Emailv31.Message.TEMPLATELANGUAGE, true)
.put(Emailv31.Message.SUBJECT, "[[data:objet:\" anything\"]]")
.put(Emailv31.Message.VARIABLES, new JSONObject()
.put("objet", " objet")
.put("mailTitle", mailTitle)
.put("body", body))));
}
private JSONArray getRecipientsJson(){
JSONArray json = new JSONArray();
for (String recipient : recipients) {
json.put(new JSONObject()
.put("Email", recipient)
.put("Name", ""));
}
return json;
}
public MailjetResponse sendLoggedMail() throws Exception {
MailjetClient client = new MailjetClient(API_KEY_PUBLIC, API_KEY_PRIVATE, new ClientOptions("v3.1"));
MailjetRequest request = sendEmailCore();
MailjetResponse response = null;
try {
client.setDebug(MailjetClient.VERBOSE_DEBUG);
log.info(request.getBody());
response = client.post(request);
}  catch (final MailjetException | MailjetSocketTimeoutException e) {
log.error("sendLoggedMail", e);
} 
return response;
}

The MailJet log :

=== HTTP Request ===
POST https://api.mailjet.com/v3.1/send
Accept-Charset:UTF-8
Accept:application/json
user-agent:mailjet-apiv3-java/v4.5.0
Content-Type:application/json
=== HTTP Response ===
Receive url: https://api.mailjet.com/v3.1/send
Status: 400
date:Tue, 01 Sep 2020 08:59:55 GMT
null:HTTP/1.1 400 Bad Request
content-length:177
x-mj-request-guid:0bb2a0ac-849d-4d80-82ed-a10a792e8d19
content-type:application/json; charset=UTF-8
Content:
null

Thank you for your help.

答案1

得分: 1

默认连接超时的值在4.5.0版本中过小。

您可以尝试在客户端选项中设置值,如下所示:

new MailjetClient("public", "secret", new ClientOptions(8000));

或者直接使用更新的版本,其中已设置了默认超时值。

英文:

The default value for the connection timeout is too small in version 4.5.0

You can try to set value in client options like this:

new MailjetClient("public", "secret", new ClientOptions(8000));

or just use the newer version, where the default timeout value is set.

答案2

得分: -1

我在Java Mailjet客户端4.5.0上遇到了相同的问题。降级到v4.4.0时,这个错误就不会出现。

英文:

I have the same issue with Java Mailjet Client 4.5.0. Downgrading to v4.4.0 this error isn't there.

huangapple
  • 本文由 发表于 2020年9月1日 20:05:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63687323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定