最近,在使用搜狐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

使用搜狐Sendcloud的Webapi发送邮件:Jodd和Apache Httpclient的更多相关文章

  1. 山寨Unity3D?搜狐畅游的免费开源游戏引擎Genesis-3D

    在CSDN上看到了<搜狐畅游发布3D游戏引擎Genesis-3D 基于MIT协议开源>(http://www.csdn.net/article/2013-11-21/2817585-cha ...

  2. jquery仿搜狐投票动画代码

    体验效果:http://hovertree.com/texiao/jquery/21/ 这是一款基于jquery实现的仿搜狐投票动画特效源码,运行该源码可见VS图标首先出现在中间位置,紧接着随着投票比 ...

  3. crawler4j源码学习(1):搜狐新闻网新闻标题采集爬虫

    crawler4j是用Java实现的开源网络爬虫.提供了简单易用的接口,可以在几分钟内创建一个多线程网络爬虫.下面实例结合jsoup,采集搜狐新闻网(http://news.sohu.com/)新闻标 ...

  4. 搜狗输入法弹出搜狐新闻的解决办法(sohunews.exe)

    狗输入法弹出搜狐新闻的解决办法(sohunews.exe) 1.找到搜狗输入法的安装目录(一般是C:\program files\sougou input\版本号\)2.右键点击sohunews.ex ...

  5. centos6.5适用的国内yum源:网易、搜狐

    设置方法如下: 1,进入yum源配置目录cd /etc/yum.repos.d 2,备份系统自带的yum源mv CentOS-Base.repo CentOS-Base.repo.bak 下载163网 ...

  6. Python伪开发者对于搜狐云景的测评

    Python伪开发者对于搜狐云景的测评 本人是GAE和OpenShift的狂热爱好者,玩过各种国外PaaS.某次想搞个稍微复杂点的Python Web程序,需要比较好的网络传输速度,就试图找前PM(P ...

  7. 搜狐云景paas平台实践之路

    前言: 搜狐云景作为搜狐的paas平台,在2014年5月22日的云计算大会上正式发布了公测.初测,注册用户必须先申请邀请码参与公测会赠送用户100元电子券,经过实名认证之后会再赠送100电子券,目测可 ...

  8. SAE、搜狐云景和百度云之初见

    近期有需求将我们的应用部署到公有云的服务平台上,于是找了几家公有云服务做了一下调研, 首先对比一下他们提供的功能: 功能 SAE 搜狐云景 百度云 版本控制工具 svn  GIT,和百度云的比起来,用 ...

  9. 继网易博客后搜狐博客也增加了nofollow标签

    继网易博客后搜狐博客也增加了nofollow标签 今天在搜狐博客发表了篇文章,在末端添加上我的版权,结果回头查看是发现,这个锚文本被加上了nofollow标签,也就是说这样的外链已经没有传递权重的作用 ...

随机推荐

  1. [CSS] Build a Fluid Loading Animation in CSS

    In this lesson, we will create a fluid loading animation using Animations and Transformations in CSS ...

  2. php编译参数注释

    1. 指定安装路径 --prefix=PREFIX 2. 指定运行用户 --with-fpm-user=nginx 3. 指定运行组 --with-fpm-group=nginx 3.与'--pref ...

  3. Injection of autowired dependencies failed; autowire 自动注入失败,测试类已初始化过了Spring容器。

    1 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creat ...

  4. ThinkPHP5.0---删除数据

    删除特定记录 public function delete() { // 获取要删除的对象:关键字为16 $Teacher = Teacher::); // 删除对象 $Teacher->del ...

  5. ThinkPHP5.0---URL访问

    ThinkPHP 5.0 在没有启用路由的情况下典型的URL访问规则是(采用 PATH_INFO 访问地址): http://serverName/index.php(或者其它应用入口文件)/模块/控 ...

  6. 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…

    https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...

  7. 防止登录页面出如今frame中

    在使用frame页面嵌套开发的时候,遇到重新启动了server的时候会出现登录页面在frame页面中出现, 所以须要在登录页面里面用js推断下当前的地址信息,然后跳转到登录的单独页面中. js代码例如 ...

  8. BigBoss按键映射

    // BigBoss: SBSettings Toggle Spec 按键映射 http://thebigboss.org/guides-iphone-ipod-ipad/sbsettings-tog ...

  9. C#数据池

    //ThreadPool(线程池)是一个静态类,它没有定义任何的构造方法(),我们只能够使用它的静态方法,这是因为,这是因为ThreadPool是托管线程池(托管线程池http://msdn.micr ...

  10. Centos 6 DNS Server 配置

    安装bind yum install -y bind bind-chroot bind-utis 如果是Centos 5 # yum -y install bind caching-nameserve ...