Android不发送邮件

时间:2013-08-10 12:20:43

标签: java android json email

我正在创建一个应用程序,它从编辑文本中获取输入并根据信息发送邮件。我正在使用Http post来执行此操作我想与 Php 脚本进行通信以发送邮件请看看我的代码..

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

我的活动:

package dolphin.developers.com;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import dolphin.devlopers.com.R;

public class misc1 extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eamilspoof);

        EditText textw =(EditText) findViewById(R.id.editText1);
        final String  strd = textw.getText().toString();

        EditText textw3 =(EditText) findViewById(R.id.editText2);
        final String  strd3 = textw3.getText().toString();

        EditText textw3d =(EditText) findViewById(R.id.editText3d);
        final String  strd3d = textw3d.getText().toString();

        EditText textw3dd =(EditText) findViewById(R.id.editText3);
        final String  name = textw3d.getText().toString();

        EditText textw3df =(EditText) findViewById(R.id.editText4);
        final String  subject = textw3d.getText().toString();

        Button pds = (Button)findViewById(R.id.button1d);
        pds.setOnClickListener(new View.OnClickListener() {

          public void onClick(View view) {

              String site = "www.dolphin123.net78.net/mailer.php";
              String namer1 = name;
              String to = strd;
              String from = strd3;
              String subject1 = subject;
              String  message = strd3d;


              String content = "";

              try
              {               
                /* Sends data through a HTTP POST request */
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(site);

                List <NameValuePair> params = new ArrayList <NameValuePair> ();
                params.add(new BasicNameValuePair("name", namer1));
                params.add(new BasicNameValuePair("to", to));
                params.add(new BasicNameValuePair("from", from));
                params.add(new BasicNameValuePair("subject", subject1));
                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();
              }
            }
          });
        };
    }
}

Logcat:

08-10 17:55:20.405: W/System.err(12988): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.dolphin123.net78.net/mailer.php
08-10 17:55:20.405: W/System.err(12988):    at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
08-10 17:55:20.405: W/System.err(12988):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
08-10 17:55:20.405: W/System.err(12988):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
08-10 17:55:20.405: W/System.err(12988):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
08-10 17:55:20.410: W/System.err(12988):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-10 17:55:20.410: W/System.err(12988):    at dolphin.developers.com.misc1$1.onClick(misc1.java:87)
08-10 17:55:20.410: W/System.err(12988):    at android.view.View.performClick(View.java:4211)
08-10 17:55:20.410: W/System.err(12988):    at android.view.View$PerformClick.run(View.java:17267)
08-10 17:55:20.410: W/System.err(12988):    at android.os.Handler.handleCallback(Handler.java:615)
08-10 17:55:20.410: W/System.err(12988):    at android.os.Handler.dispatchMessage(Handler.java:92)

新日志:

08-05 13:08:17.365: D/SntpClient(73): request time failed: java.net.SocketException: Address family not supported by protocol

1 个答案:

答案 0 :(得分:2)

将协议添加到String的{​​{1}}表示中。

所以它应该是:“http://www.dolphin123.net78.net/mailer.php

您可以使用前缀为URI

的上方网址添加您的网址