简体   繁体   中英

Error when I trying to send email with Mandrill SMTP in android

Im using Android Studio 1.2 when run my app the API Mandrill response with this error

{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a message value"}

When I inspected my object paramsArr It seems okay, but I think that may be I miss some property or my paramsArr doesn't have the correct structure.

this is the strucure of 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" /> 

function that send email

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

                    }


    }

The problem was in the structure of my parameter 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}]

the correct structure is

{"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}

With this function you can send and email from android using this service, I tested and it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM