公司里的同事写的在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();
}
}
}