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

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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大