最近,在使用搜狐Sendcloud发邮件。
    Sendcloud提供http格式的webapi,方便地发送邮件,当然是要付费的。

很早之前,http工具一直用Httpclient,后来觉得jodd更简单,就倾向于jodd的一些工具库了。

使用jodd遇到一个问题:
  当邮件内容比较大时,比如1万多字符的时候,发送邮件失败。
Sendcloud服务器所在的Nginx,提示

414 Request-URI Too Large


“<html>

<head><title>414 Request-URI Too Large</title></head>

<body bgcolor="white">

<center><h1>414 Request-URI Too Large</h1></center>

<hr><center>nginx</center>

</body>

</html>”

提交工单,与客服和技术支持,交流了几个小时,终于解决了问题。

第1种方法:使用官方给的Apache Httpclient的例子,发送邮件。
第2种方法:原来用Jodd,使用方式有问题。

Map<String, String> queryMap = new HashMap<String, String>();
queryMap.put("api_user", API_USER);
queryMap.put("api_key", API_KEY);
queryMap.put("from", FROM);
queryMap.put("to", to);
queryMap.put("subject", subject);
queryMap.put("html", html.substring(0,html.length()));
HttpResponse response = HttpRequest.post(URL)// .contentType(contentType)
.query(queryMap).send();
String body = response.bodyText();

这个地方用的是“post” ,但是参数仍然放在了url后面,当数据量过大时,就有问题了。
正确的做法是: HttpResponse response = HttpRequest.post(URL)// .contentType(contentType)

.form(queryMap).send();
用form方法替代query方法。

有2个疑惑:
1.用post发送,为啥会把参数放在url后面?或者说,url后面接参数,还是post发送么?
2. jodd官方,有这句话:
    Query parameters may be specified in the URL line (but then they have to be correctly encoded).
   为啥是“可能”?

以下是一些代码

Apache发送:

public static void send(String to, String subject, String html) {
if (!check()) {
return;
}
String url = URL;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(url); List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("api_user", API_USER));
nvps.add(new BasicNameValuePair("api_key", API_KEY));
nvps.add(new BasicNameValuePair("from", FROM));
nvps.add(new BasicNameValuePair("to", to));
nvps.add(new BasicNameValuePair("subject", subject));
nvps.add(new BasicNameValuePair("html", html));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse response = httpclient.execute(httpost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 正常返回
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
} else {
System.err.println("error");
} } catch (Exception e) {
e.printStackTrace();
} }

Jodd发送:

public static void send(String to, String subject, String html) {
if (!check()) {
return;
}
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("api_user", API_USER);
queryMap.put("api_key", API_KEY);
queryMap.put("from", FROM);
queryMap.put("to", to);
queryMap.put("subject", subject);
queryMap.put("html", html.substring(0,html.length()));
HttpResponse response = HttpRequest.post(URL)// .contentType(contentType)
.form(queryMap).send();
String body = response.bodyText();
try {
JSONObject jsonObject = JSONObject.parseObject(body);
MailMessage msg = JSONObject.toJavaObject(jsonObject,
MailMessage.class); String sendInfo = "to=" + to + ",subject=" + subject;
if (msg.getMessage().equals(MailMessage.ERROR)) {
logger.error("sendcloud,send mail failed:" + msg + ",sendInfo:"
+ sendInfo);
} else if (msg.getMessage().equals(MailMessage.SUCCESS)) {
logger.info("sendcloud,send mail ok,sendInfo:" + sendInfo);
}
} catch (Exception e) {
logger.error("send mail failed",e);
logger.info(body);
} // System.out.println(response);
}

jodd资料:http://jodd.org/doc/http.html

最新文章

  1. 可以结合react的ui组件
  2. mybatis——使用mapper代理开发方式
  3. 编译gcc
  4. eclipse下tomcat插件配置说明
  5. 【分割圆】Uva 10213 - How Many Pieces of Land ?
  6. 读取properties文件
  7. android LinearLayout和RelativeLayout实现精确布局
  8. Java层与Jni层的数组传递(转)
  9. DATA VISUALIZATION – PART 1
  10. (视频) 《快速创建网站》 3.2 WordPress多站点及Azure在线编辑器 - 扔掉你的ftp工具吧,修改代码全部云端搞定
  11. 【RL-TCPnet网络教程】第35章 FTP文件传输协议基础知识
  12. webclient 操作超时
  13. windows_agent 添加
  14. BZOJ3597 SCOI2014方伯伯运椰子(分数规划+spfa)
  15. 【EatBook】-NO.2.EatBook.2.JavaArchitecture.1.001-《修炼Java开发技术在架构中体验设计模式和算法之美》-
  16. 配置url防盗链、目录权限访问控制Directory、文件访问权限控制FilesMatch
  17. GL_ACTIVE_UNIFORMS可能不会返回没有用到的uniform
  18. Volley 解析
  19. javascript 高级选择器:querySelector 和 querySelectorAll
  20. 用Squid实现反向代理

热门文章

  1. Robot Framework初步使用
  2. 在安卓(手机)上运行 Ubuntu (Linux)
  3. SQL查询练习二(From LeetCode)
  4. &quot;C:\Program Files\Internet Explorer\iexplore.exe&quot; -extoff 无加载项启动IE 浏览器打开时全屏模式
  5. 11. Spring Boot JPA 连接数据库
  6. JS学习笔记 - fgm练习 - 输入法下拉框 三元表达式
  7. 应用Python来计算排列中的逆序数个数
  8. C语言深度剖析-----指针数组和数组指针的分析
  9. 14、编写一个通用的Makefile
  10. Aamazon Web Service EC2 Ubuntu 新建用户而且用ssh连接host