猿问

从 Android 调用邮件 PHP 文件不起作用

我的 Android 应用程序具有以下功能:


void sendEmail(String PHPfileUurl, String receiverEmail, String fromEmail) {

        ParseUser currentUser = ParseUser.getCurrentUser();


        StringBuilder messageBuilder = new StringBuilder();

        for (int i=0; i<productsOrdered.size(); i++){

            messageBuilder.append(productsOrdered.get(i)).append("\n");

        }

        String mess = messageBuilder.toString();


        String parameters = "name=" + currentUser.getString(Configurations.USER_FULLNAME) +

                "&fromEmail=" + fromEmail +

                "&receiverEmail=" + receiverEmail +

                "&messageBody=" + mess +

                "&storeName=" + Configurations.MERCHANT_NAME +

                "&shippingAddress=" + currentUser.getString(Configurations.USER_SHIPPING_ADDRESS);


        String strURL = PHPfileUurl + parameters;

        strURL = strURL.replace(" ", "%20");

        strURL = strURL.replace("\n", "%20");


        Log.i(Configurations.TAG, "PHP STRING URL: " + strURL);


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);

        try {

            URL url;

            url = new URL(strURL);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("POST");

            conn.setConnectTimeout(20000);

    }


所以我认为一切都很好,因为 RESPONSE = OK。但事实并非如此,因为我不会在 admin@mydomain.com 收到任何电子邮件(还有另一个电子邮件地址,我已经发布了一个假的作为示例,Logcat 将我的真实电子邮件地址打印为receiverEmail)。


我的代码有问题吗?还有另一种方法可以从我自己的服务器调用mail.php文件吗?我也试过这个问题,但我不能DefaultHttpClient在我的项目中导入这个类。


HUX布斯
浏览 200回答 2
2回答

侃侃无极

使用 $_GET 而不是 $_POST ,将所有变量从$name&nbsp;=&nbsp;$_POST['name'];到$name&nbsp;=&nbsp;$_GET['name'];

绝地无双

如果您将 $_POST 更改为 $_GET 会更容易,但如果消息中有 (&something=),则 $_GET 方法中的问题您将只会收到一半的消息,因为 &something= 将设置为另一个 $_GET , 如果消息太长,您也可能会遇到一些问题,所以如果你想使用 $_POST 方法而不是 $_GET您需要更改您的java代码,确保导入Map,然后将其更改为此void sendEmail(String PHPfileUurl, String receiverEmail, String fromEmail) {&nbsp; &nbsp; ParseUser currentUser = ParseUser.getCurrentUser();&nbsp; &nbsp; StringBuilder messageBuilder = new StringBuilder();&nbsp; &nbsp; for (int i=0; i<productsOrdered.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; messageBuilder.append(productsOrdered.get(i)).append("\n");&nbsp; &nbsp; }&nbsp; &nbsp; String mess = messageBuilder.toString();&nbsp; &nbsp; Map<String,Object> params = new LinkedHashMap<>();params.put("name", currentUser.getString(Configurations.USER_FULLNAME));params.put("fromEmail", fromEmail);params.put("receiverEmail", receiverEmail);params.put("messageBody", mess);&nbsp;params.put("storeName", Configurations.MERCHANT_NAME);&nbsp; params.put("shippingAddress", currentUser.getString(Configurations.USER_SHIPPING_ADDRESS);StringBuilder postData = new StringBuilder();for (Map.Entry<String,Object> param : params.entrySet()) {&nbsp; &nbsp; if (postData.length() != 0) postData.append('&');&nbsp; &nbsp; postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));&nbsp; &nbsp; postData.append('=');&nbsp; &nbsp; postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));}byte[] postDataBytes = postData.toString().getBytes("UTF-8");&nbsp; &nbsp; String strURL = PHPfileUurl;&nbsp; &nbsp; StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();&nbsp; &nbsp; StrictMode.setThreadPolicy(policy);&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; URL url;&nbsp; &nbsp; &nbsp; &nbsp; url = new URL(strURL);&nbsp; &nbsp; &nbsp; &nbsp; HttpURLConnection conn = (HttpURLConnection) url.openConnection();&nbsp; &nbsp; &nbsp; &nbsp; conn.setRequestMethod("POST");&nbsp; &nbsp; &nbsp; &nbsp; conn.setConnectTimeout(20000);&nbsp; &nbsp; &nbsp; &nbsp; conn.setReadTimeout(20000);&nbsp; &nbsp; &nbsp; &nbsp; conn.setDoInput(true);&nbsp; &nbsp; &nbsp; &nbsp; conn.setDoOutput(true);&nbsp; &nbsp; &nbsp; &nbsp; conn.getOutputStream().write(postDataBytes);&nbsp; &nbsp; &nbsp; &nbsp; if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream is = conn.getInputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i(Configurations.TAG, "EMAIL RESPONSE: " + conn.getResponseMessage());&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream err = conn.getErrorStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i(Configurations.TAG, "ERROR ON EMAIL: " + err);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (IOException e) {e.printStackTrace(); }}
随时随地看视频慕课网APP

相关分类

Java
我要回答