我尝试在Android中使用Mandrill SMTP发送电子邮件时出错

时间:2015-06-21 13:21:44

标签: java android email mandrill

我使用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}]

摇篮:

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();

                    }


    }

1 个答案:

答案 0 :(得分:1)

问题出在我的参数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发送和发送电子邮件,我测试过它可以正常工作。