【问题标题】:MailJet Not able to send from java email: ( 400 Bad Request )MailJet 无法从 java 电子邮件发送:( 400 Bad Request )
【发布时间】:2020-12-20 13:09:15
【问题描述】:

我尝试从我们的 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代码:

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

感谢您的帮助。

【问题讨论】:

    标签: java mailjet


    【解决方案1】:

    4.5.0版本连接超时默认值太小

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

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

    或者只使用较新的版本,其中设置了默认超时值。

    【讨论】:

      【解决方案2】:

      Java Mailjet Client 4.5.0 也有同样的问题。降级到 v4.4.0 这个错误就不存在了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-29
        • 1970-01-01
        • 2012-11-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多