通过 Java 发送电子邮件模板时的 Mandrill-Error code-5

Posted

技术标签:

【中文标题】通过 Java 发送电子邮件模板时的 Mandrill-Error code-5【英文标题】:Mandrill-Error code-5 while sending email template through Java 【发布时间】:2015-09-10 12:05:06 【问题描述】:

我正在使用 mandrill api 在 java 中发送模板电子邮件,但出现以下错误:

com.cribbstechnologies.clients.mandrill.exception.RequestFailedException: 失败: HTTP 错误代码: 500 "status":"error","code":5,"name":"Unknown_Template","message":"没有这样的模板\"Hello\""

我的代码是:

public static void main(String[] args) 
        config.setApiKey("Your_Api_Key");
        config.setApiVersion("1.0");
        config.setBaseURL("https://mandrillapp.com/api");

        request.setConfig(config);
        request.setObjectMapper(mapper);
        messagesRequest.setRequest(request);
        client = new DefaultHttpClient();
        request.setHttpClient(client);

        //MandrillMessageRequest mmr = new MandrillMessageRequest();
        MandrillhtmlMessage message = new MandrillHtmlMessage();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("X-MC-TrackingDomain", "http://executeproject.com:8585/OctopusMailTest/track?cmpid=1");
        message.setFrom_email("noreply@octopusmail.com");
        message.setFrom_name("Anand");
        message.setHeaders(headers);
        //message.setHtml("<html><body><h1>Please Open this mail and click any where.i will track your click.</h1>Don't wait click google?<a href=\"http://www.google.com\">google</a></body></html>");
        message.setSubject("Good Afternoon !");

        MandrillRecipient[] recipients = new MandrillRecipient[]new MandrillRecipient("anand","anandkumar.m@softidsolutions.com"), new MandrillRecipient("rakesh","rakesh.s@softidsolutions.com"),new MandrillRecipient("Ravi","ravi.k@softidsolutions.com");
        message.setTo(recipients);
        /*message.setTrack_clicks(true);
        message.setTrack_opens(true);*/
        //mmr.setMessage(message);


        MandrillTemplatedMessageRequest mtmr=new MandrillTemplatedMessageRequest();
        mtmr.setTemplate_name("Hello");
        TemplateContent template=new TemplateContent();
        List<TemplateContent>template_content=new ArrayList<TemplateContent>();
        template.setName("First Block");
        template.setContent("<h1>Helllo You are in First Block<br/>");
        template_content.add(template);
        template.setName("Second Block");
        template.setContent("<h1>Helllo You are in Second Block");
        template_content.add(template);
        mtmr.setTemplate_content(template_content);
        mtmr.setMessage(message);
        try 
            messagesRequest.sendTemplatedMessage(mtmr);
            //SendMessageResponse response = messagesRequest.sendMessage(mmr);
            System.out.println("Message sent Successfully");

         catch (RequestFailedException e) 
            e.printStackTrace();

         
    

【问题讨论】:

您是否真的added在您的 Mandrill 帐户中发布了模板? 我想通过 API 调用添加模板。未添加到 Mandrill 帐户中。 【参考方案1】:

试试这个。

MandrillApi mandrillApi = new MandrillApi("<Mandrill API here>");
MandrillTemplate newTemplate = mandrillApi.templates().add(
    "test_template_001", 
    "<html><body><h1>Hello World!</h1></body></html>",
    false);

【讨论】:

以上是关于通过 Java 发送电子邮件模板时的 Mandrill-Error code-5的主要内容,如果未能解决你的问题,请参考以下文章

如何解析 Thymeleaf 模板以通过电子邮件发送?

模板发送java邮件

Node.Js AWS SES - 发送电子邮件时的 ETIMEDOUT

html 通过kentico电子邮件引擎/模板/队列发送电子邮件

java发送html模板的高逼格邮件

通过apex发送时,合并字段值不会显示在电子邮件模板中