我在 android 中尝试使用 Mandrill SMTP 发送电子邮件时出错
Error when I trying to send email with Mandrill SMTP in android
我正在使用 Android Studio 1.2,当 运行 我的应用 API Mandrill 响应时出现此错误
{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a message value"}
当我检查我的对象 paramsArr 时它似乎没问题,但我认为可能是我错过了一些 属性 或者我的 paramsArr 没有正确的结构。
这是paramsArr的结构:
[{"key":"mykey","message":[{"html":"ghgh","text":"test","to":[{"email":"mymail@gmail.com","name":"person","type":"to"}],"from_email":"mymail@gmail.com","from_name":"person","subject":"erer"}],"async":false}]
Gradle:
compileSdkVersion 21
buildToolsVersion '22.0.1'
minSdkVersion 16
targetSdkVersion 21
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
发送邮件功能
public void SendEmail(View view) {
try {
JSONArray paramsArr = new JSONArray();
JSONArray paramsArr1 = new JSONArray();
JSONArray paramsArr2 = new JSONArray();
JSONObject object = new JSONObject();
object.put("key", "mykey");
JSONObject arrayElementTo = new JSONObject();
arrayElementTo.put("email","mymail@gmail.com");
arrayElementTo.put("name", "person");
arrayElementTo.put("type", "to");
paramsArr1.put(arrayElementTo);
JSONObject arrayElementMessage = new JSONObject();
arrayElementMessage.put("html", "ghgh");
arrayElementMessage.put("text", "test");
arrayElementMessage.put("to",paramsArr1);
arrayElementMessage.put("from_email", "mymail@gmail.com");
arrayElementMessage.put("from_name", "person");
arrayElementMessage.put("subject", "erer");
paramsArr2.put(arrayElementMessage);
object.put("message",paramsArr2);
object.put("async", false);
paramsArr.put(object);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://mandrillapp.com/api/1.0/messages/send.json");
StringEntity params = new StringEntity(paramsArr.toString());
httppost.setHeader("Content-type", "application/json");
httppost.setEntity(params);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
}catch (Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "error",
Toast.LENGTH_SHORT);
toast.show();
}
}
问题出在我的参数结构中 json [{"key":"mykey","message":[{"html":"ghgh","text":"test","to":[{"email":"mymail@gmail.com","name":"person","type":"to"}],"from_email":"mymail@gmail.com","from_name":"person","subject":"erer"}],"async":false}]
正确的结构是
{"key":"mykey","message":{"html":"ghgh","text":"test","to":[{"email":"mymail@gmail.com","name":"person","type":"to"}],"from_email":"mymail@gmail.com","from_name":"person","subject":"erer"},"async":false}
通过此功能,您可以使用此服务从 android 发送电子邮件,我测试过它工作正常。
我正在使用 Android Studio 1.2,当 运行 我的应用 API Mandrill 响应时出现此错误
{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a message value"}
当我检查我的对象 paramsArr 时它似乎没问题,但我认为可能是我错过了一些 属性 或者我的 paramsArr 没有正确的结构。
这是paramsArr的结构:
[{"key":"mykey","message":[{"html":"ghgh","text":"test","to":[{"email":"mymail@gmail.com","name":"person","type":"to"}],"from_email":"mymail@gmail.com","from_name":"person","subject":"erer"}],"async":false}]
Gradle:
compileSdkVersion 21
buildToolsVersion '22.0.1'
minSdkVersion 16
targetSdkVersion 21
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
发送邮件功能
public void SendEmail(View view) {
try {
JSONArray paramsArr = new JSONArray();
JSONArray paramsArr1 = new JSONArray();
JSONArray paramsArr2 = new JSONArray();
JSONObject object = new JSONObject();
object.put("key", "mykey");
JSONObject arrayElementTo = new JSONObject();
arrayElementTo.put("email","mymail@gmail.com");
arrayElementTo.put("name", "person");
arrayElementTo.put("type", "to");
paramsArr1.put(arrayElementTo);
JSONObject arrayElementMessage = new JSONObject();
arrayElementMessage.put("html", "ghgh");
arrayElementMessage.put("text", "test");
arrayElementMessage.put("to",paramsArr1);
arrayElementMessage.put("from_email", "mymail@gmail.com");
arrayElementMessage.put("from_name", "person");
arrayElementMessage.put("subject", "erer");
paramsArr2.put(arrayElementMessage);
object.put("message",paramsArr2);
object.put("async", false);
paramsArr.put(object);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://mandrillapp.com/api/1.0/messages/send.json");
StringEntity params = new StringEntity(paramsArr.toString());
httppost.setHeader("Content-type", "application/json");
httppost.setEntity(params);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
}catch (Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "error",
Toast.LENGTH_SHORT);
toast.show();
}
}
问题出在我的参数结构中 json [{"key":"mykey","message":[{"html":"ghgh","text":"test","to":[{"email":"mymail@gmail.com","name":"person","type":"to"}],"from_email":"mymail@gmail.com","from_name":"person","subject":"erer"}],"async":false}]
正确的结构是
{"key":"mykey","message":{"html":"ghgh","text":"test","to":[{"email":"mymail@gmail.com","name":"person","type":"to"}],"from_email":"mymail@gmail.com","from_name":"person","subject":"erer"},"async":false}
通过此功能,您可以使用此服务从 android 发送电子邮件,我测试过它工作正常。