使用搜狐Sendcloud的Webapi发送邮件:Jodd和Apache Httpclient

最近,在使用搜狐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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值