whateverXk 2015-09-01 08:35 采纳率: 0%
浏览 2258
已结题

java使用 Mandrill平台 发送邮件总是500

公司里的同事写的在mandrill平台发送email模板,,,注意是用的https://mandrillapp.com/api/1.0/messages/send-template.json 接口;
我参照官方文档https://www.mandrillapp.com/api/docs/messages.JSON.html
以及参照他的代码写了个简单的使用模板发送email的小程序,但总是报:

HTTP/1.1 500 Internal Server Error [Server: nginx/1.6.3, Date: Tue, 01 Sep 2015 08:25:15 GMT, Content-Type: application/json; charset=utf-8, Transfer-Encoding: chunked, Access-Control-Allow-Origin: *, Access-Control-Allow-Methods: POST, GET, OPTIONS, Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Credentials: false] org.apache.http.conn.BasicManagedEntity@728d4682

后来我又用https://mandrillapp.com/api/1.0/messages/send.json 发送简单的message就成功了。
以下是我发送模板的代码:
package org.sms.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import com.google.gson.Gson;
public class Test {

public static void main(String[] args) {
    try {
        Map<String,Object> params=new HashMap<String,Object>();


        List<Map> template_contents=new ArrayList<Map>();
         Map<String,String> template_content=new HashMap<String,String>();
         template_content.put("name","SUBJECT");  
         template_content.put("content","Welcome");  
         template_contents.add(template_content);
         params.put("template_content",template_contents);  


         Map<String,Object> message=new HashMap<String,Object>();
         message.put("html","Welcome");  
         message.put("subject","Welcome Sign Up");  
         message.put("from_email","xukai59740@outlook.com");  
         message.put("preserve_recipients", true);
         message.put("from_name","servishero");  


         List<Map> merge_vars=new ArrayList<Map>();
         Map<String,Object> merge_var=new HashMap<String,Object>();
         merge_var.put("rcpt","xukai59740@outlook.com");  
         merge_vars.add(merge_var);  
         message.put("merge_vars",merge_vars);

         List<String> tags=new ArrayList<String>();
         tags.add("");  
         message.put("tags",tags);


         List<Map> tos=new ArrayList<Map>();
         Map<String,String> to=new HashMap<String,String>();
         to.put("email","xukai59740@outlook.com");  
         tos.add(to);  
         message.put("to",tos);

         params.put("async",false);  
         params.put("message",message);  
         params.put("key","******************");  
        params.put("template_name","New Customer - Welcome Email Verification");  

        Gson gson=new Gson();
        String url="https://mandrillapp.com/api/1.0/messages/send-template.json";
        //String  url="https://mandrillapp.com/api/1.0/messages/send.json";

        HttpClient client = new DefaultHttpClient();    
        HttpPost post = new HttpPost(url);    

        StringEntity s = new StringEntity(gson.toJson(params));    
        s.setContentEncoding("UTF-8");    
        s.setContentType("application/json");    
        post.setEntity(s);    


        HttpResponse res = client.execute(post);    

        if(res.getStatusLine().getStatusCode() == 200){    
            System.out.println( EntityUtils.toString(res.getEntity())   );
        }   else{
            System.out.println(res.toString());
        }
    }catch (Exception e) {
        e.printStackTrace();
    }
}

}

  • 写回答

1条回答 默认 最新

  • devmiao 2015-09-03 16:30
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error