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

时间:2015-06-24 10:19:45

标签: java mandrill

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

  

com.cribbstechnologies.clients.mandrill.exception.RequestFailedException:失败:HTTP错误代码:500 {" status":"错误","代码": 5,"名称":" Unknown_Template","消息":"没有这样的模板\" Hello \"&#34 ;}

我的代码是:

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();

        } 
    }

1 个答案:

答案 0 :(得分:0)

试试这个。

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