捕获用户输入并使用按钮将其直接发送到电子邮件地址

时间:2011-06-06 15:49:35

标签: android email android-intent

我目前正在开发一个有订单的Android应用程序。我想知道如何允许应用程序捕获用户输入的内容并使用按钮将输入直接发送到电子邮件,而无需将用户带到电子邮件应用页面。我知道我的.java类有错误,但是,我正在尝试...下面是我的xml(布局)文件和我的.java类文件。请帮帮我...谢谢!!

这是我的.xml文件(布局)     

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:stretchColumns="1">
<TextView
    android:id="@+id/txtname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name: "
    />
<EditText
    android:id="@+id/editname"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtname"
    android:hint="Your name please"
    />
<TextView
    android:id="@+id/txtemail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editname"
    android:text="Email Address: "
    />
<EditText
    android:id="@+id/editemail"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtemail"
    android:hint="Enter email address here"
    />
<TextView
    android:id="@+id/txtnum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editemail"
    android:text="Contact Number: "
    />
<EditText
    android:id="@+id/editnum"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtnum"
    android:hint="Enter contact number here"
    />
<TextView
    android:id="@+id/txtadd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editnum"
    android:text="Mailing Address: "
    />
<EditText
    android:id="@+id/editadd"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtadd"
    android:hint="Enter mailing address here"
    />
<TextView
    android:id="@+id/txtitems"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editadd"
    android:text="Item(s): "
    />
<EditText
    android:id="@+id/edititems"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtadd"
    android:hint="Enter item(s) here"
    />
<Button
    android:id="@+id/btn_submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/edititems"
    android:text="Submit form"
    />
</TableLayout>

这是我的.java类代码

public class OrderFormActivity extends Activity implements OnClickListener{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.orderform);

    Button btnsubmit = (Button)findViewById(R.id.btn_submit);
    btnsubmit.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == this.findViewById(R.id.btn_submit)) {

    EditText name = (EditText)findViewById(R.id.editname);
    String strname = name.getText().toString();

    EditText email = (EditText)findViewById(R.id.editemail);
    String stremail = email.getText().toString();

    EditText num = (EditText)findViewById(R.id.editnum);
    String strnum = num.getText().toString();

    EditText add = (EditText)findViewById(R.id.editadd);
    String stradd = add.getText().toString();

    EditText items = (EditText)findViewById(R.id.edititems);
    String stritems = items.getText().toString();

    showOrder(strname, stremail, strnum, stradd,stritems);
    }

}

private void showOrder (String strname, String stremail, String strnum, String stradd, String stritems) {
    String result = strname + stremail + strnum + stradd + stritems;

    //Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

    String emailAddress = "kwok.winona@gmail.com";

    Intent intent = new Intent(Intent.ACTION_SEND);  
    intent.setType("plain/text");
    intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);    
    intent.putExtra(Intent.EXTRA_TEXT, result);         

}

}

提前再次感谢所有人! :D

1 个答案:

答案 0 :(得分:2)

没有API可以在没有用户互动的情况下通过意图发送电子邮件。这已经在SO中重复了几次。

正如pearsonartphoto建议的那样,您需要使用外部库来发送电子邮件。 JavaMail是明确的第一选择,即使您“发现该解决方案过于繁琐”。

所以你需要收集你发布的所有字段,以有用的方式格式化电子邮件(也许是xml,也许是JSON?),然后用JavaMail将其作为文本发送。