记录一些使用问题
官网地址http://blog.sendcloud.net/
常用的是普通发送和模板发送,下列代码以模板发送为例
模板发送工具类
public int send_template_maillist(String useremail,String code) throws ClientProtocolException, IOException {
final String url = "http://api.sendcloud.net/apiv2/mail/sendtemplate";
int msg =0;
final String apiUser = "用户名";
final String apiKey = "用户密钥";
/**
*拼接的参数串
*/
String xsmi="{\"to\": ["+useremail+"],\"sub\":{\"%name%\": [\"Ben\"],\"%code%\":["+code+"]}}";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("apiUser", apiUser));
params.add(new BasicNameValuePair("apiKey", apiKey));
params.add(new BasicNameValuePair("templateInvokeName", "模板名称"));
params.add(new BasicNameValuePair("from", "发送者的邮箱地址"));
params.add(new BasicNameValuePair("fromName", "随便的名字"));
params.add(new BasicNameValuePair("useAddressList", "false"));
params.add(new BasicNameValuePair("xsmtpapi", xsmi));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
System.out.println(EntityUtils.toString(response.getEntity()));
msg=1;
} else {
System.err.println("error");
msg=0;
}
httpPost.releaseConnection();
return msg;
}
这样是可以用qq邮箱,但是(纯字母)的邮箱比如sfjfjsfadg@sina.comm,会提示xsmtpapi格式错误。code的值只能是纯数字形式的,否则也是xsmtpapi格式错误。
String xsmi="{\"to\": ['"+useremail+"'],\"sub\":{\"%name%\": [\"Ben\"],\"%code%\":['"+code+"']}}";
在参数上再加上一个引号就可以了,转义后实际发送的是
String xsmi="{“to”: [‘qqqqqqq.@qq.com’],“sub”:{"%name%": [“Ben”],"%code%":[‘sae954’]}}";
问题解决。