MailJet无法从Java电子邮件发送: 400错误请求

问题描述

我尝试从我们的Java项目发送电子邮件,但每次遇到代码错误400。因此,我将打印在Java控制台中的相同请求从Postman发送给其他相同元素,然后在Postman和我收到了电子邮件,但从Java收到的代码总是有400错误错误请求,当我打开Mailjet调试模式时,内容为空。

这是Java制作的最终JSON,并由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\"]]"
        }
    ]
}

java代码

/**
     *  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;
    }

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

谢谢您的帮助。

解决方法

在4.5.0版本中,连接超时的默认值太小

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

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

或仅使用设置了默认超时值的较新版本。

,

我与Java Mailjet Client 4.5.0有相同的问题。降级到v4.4.0不会出现此错误。