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

Posted

技术标签:

【中文标题】当我尝试在 android 中使用 Mandrill SMTP 发送电子邮件时出错【英文标题】:Error when I trying to send email with Mandrill SMTP in android 【发布时间】:2015-09-07 00:29:45 【问题描述】:

我在运行我的应用时使用 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】:

问题出在我的参数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 中使用 Mandrill SMTP 发送电子邮件时出错的主要内容,如果未能解决你的问题,请参考以下文章

在对 mandril 的 api 调用中使用 laravel-view

未能通过 Mandril & Parse-Cloud-Code 发送超过 9 封电子邮件

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

当我尝试在 Android 11 中读取文件时出现电容器文件系统错误

当我尝试在 Android Studio 中构建项目时,如何解决此错误?

当我尝试在 Android Studio 中添加新活动时出现问题,弹出新活动选项窗口为空白/白色