如何调用php文件发送邮件? ,Android

时间:2012-03-29 11:50:36

标签: android email post

我有以下mail.php文件:

<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = "From: ".$name."\r\n";
$message .= $_POST['message'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?> 

我有以下布局从我的应用发送邮件: enter image description here

我想在点击发送按钮后发送邮件到静态邮件,例如:mymail@mail.com 如何通过调用上面的php文件发送邮件 我听说过调用上面的php文件的post方法 但对此却一无所知。
请帮忙!

2 个答案:

答案 0 :(得分:5)

这是:

    public static void sendData(String name, String to, String from, String subject, String message)
    {
        String content = "";

        try
        {               
            /* Sends data through a HTTP POST request */
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://your.website.com");
            List <NameValuePair> params = new ArrayList <NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            params.add(new BasicNameValuePair("to", to));
            params.add(new BasicNameValuePair("from", from));
            params.add(new BasicNameValuePair("subject", subject));
            params.add(new BasicNameValuePair("message", message));
            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

            /* Reads the server response */
            HttpResponse response = httpClient.execute(httpPost);
            InputStream in = response.getEntity().getContent();

            StringBuffer sb = new StringBuffer();
            int chr;
            while ((chr = in.read()) != -1)
            {
                sb.append((char) chr);
            }
            content = sb.toString();
            in.close();

            /* If there is a response, display it */
            if (!content.equals(""))
            {
                Log.i("HTTP Response", content);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

答案 1 :(得分:0)

您必须加载Web视图或编写Web服务并将参数作为JSON传递。