Java 从Android调用邮件PHP文件';行不通

Java 从Android调用邮件PHP文件';行不通,java,php,android,post,Java,Php,Android,Post,我的Android应用程序中有以下功能: void sendEmail(String PHPfileUurl, String receiverEmail, String fromEmail) { ParseUser currentUser = ParseUser.getCurrentUser(); StringBuilder messageBuilder = new StringBuilder(); for (int i=0; i<prod

我的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);
            conn.setReadTimeout(20000);
            conn.setDoInput(true);
            conn.setDoOutput(true);

            if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
                InputStream is = conn.getInputStream();
                Log.i(Configurations.TAG, "EMAIL RESPONSE: " + conn.getResponseMessage());
            } else {
                InputStream err = conn.getErrorStream();
                Log.i(Configurations.TAG, "ERROR ON EMAIL: " + err);
            }
        } catch (IOException e) {e.printStackTrace(); }
    }
因此,我假设一切正常,因为响应=OK。但事实并非如此,因为我不会在任何时候收到任何电子邮件admin@mydomain.com(还有另一个电子邮件地址,我发布了一个假地址作为示例,Logcat将我的真实电子邮件地址打印为
receiverEmail

这是我的
mail.php
文件:

// POST Variables
$name = $_POST['name'];
$fromEmail = $_POST['fromEmail'];
$receiverEmail = $_POST['receiverEmail'];
$messageBody = $_POST['messageBody'];
$storeName = $_POST['storeName'];
$shippingAddress = $_POST['shippingAddress'];
$headers = 'From: ' .$fromEmail;

// SUBJECT 
$subject = "New order from " .$name. " on '" .$storeName. "'";


// COMPOSE MESSAGE 
$message = 
"ORDER DETAILS:\n".
$messageBody.
"\n\nName: " .$name. 
"\nUser Email: " .$fromEmail.
"\nShipping Address: " .$shippingAddress
;

/* Finally send email */
mail($receiverEmail,
    $subject, 
    $message, 
    $headers
);

/* Result */
echo "Email Sent to: " .$receiverEmail. "\n Message: " .$message;

我的代码有问题吗?有没有其他方法可以从我自己的服务器调用mail.php文件?我也尝试过,但是我无法在我的项目中导入
DefaultHttpClient
类。

使用$\u GET而不是$\u POST

将所有变量从

$name = $_POST['name'];


如果您将$\u POST更改为$\u GET,会更容易 但是,如果消息中包含(&something=),则$\u GET方法中存在问题 如果将&something=设置为另一个$\u GET,您将只收到一半的消息,如果消息太长,您可能会遇到一些问题

因此,如果您想使用$\u POST方法而不是$\u GET

您需要更改java代码, 确保导入地图,然后将其更改为

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


    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);
 params.put("storeName", Configurations.MERCHANT_NAME);
  params.put("shippingAddress", currentUser.getString(Configurations.USER_SHIPPING_ADDRESS);

StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
    if (postData.length() != 0) postData.append('&');
    postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
    postData.append('=');
    postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");


    String strURL = PHPfileUurl;

    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);
        conn.setReadTimeout(20000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);


        if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
            InputStream is = conn.getInputStream();
            Log.i(Configurations.TAG, "EMAIL RESPONSE: " + conn.getResponseMessage());
        } else {
            InputStream err = conn.getErrorStream();
            Log.i(Configurations.TAG, "ERROR ON EMAIL: " + err);
        }
    } catch (IOException e) {e.printStackTrace(); }
}
void sendmail(字符串PHPfileUurl,字符串receiverEmail,字符串frommail){
ParseUser currentUser=ParseUser.getCurrentUser();
StringBuilder messageBuilder=新建StringBuilder();

对于(int i=0;iit可以工作,尽管我必须使用$\u GET实例创建一个新的PHP文件,因为我的应用程序的iOS版本调用了相同的mail.PHP文件,所以我想使用相同的文件,但似乎不行,因为我的代码需要$\u GET而不是$\u POST。无论如何,非常感谢您的帮助!如果您使用的是parse,为什么不使用云代码并使用它呢使用nodejs?使用nodejs发送电子邮件要简单得多,而且比php更安全。这只是一个想法。因为我不知道如何使用cloudcode发送电子邮件:)这是一个糟糕的借口。但不管用什么方法都可以。我试过使用Mailgun和Mandrill,但是使用php文件更复杂,所以我总是寻找最简单的解决方案;)
$name = $_GET['name'];
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();


    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);
 params.put("storeName", Configurations.MERCHANT_NAME);
  params.put("shippingAddress", currentUser.getString(Configurations.USER_SHIPPING_ADDRESS);

StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
    if (postData.length() != 0) postData.append('&');
    postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
    postData.append('=');
    postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");


    String strURL = PHPfileUurl;

    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);
        conn.setReadTimeout(20000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);


        if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
            InputStream is = conn.getInputStream();
            Log.i(Configurations.TAG, "EMAIL RESPONSE: " + conn.getResponseMessage());
        } else {
            InputStream err = conn.getErrorStream();
            Log.i(Configurations.TAG, "ERROR ON EMAIL: " + err);
        }
    } catch (IOException e) {e.printStackTrace(); }
}