5

I am currently working on an Android application which has an order form. I would like to know how I can allow the application to capture what the user has input and send the input directly to an email using button, without bringing the user to the email app page. I know my .java class has errors, however, I am trying out... Below is my xml (layout) file and my .java class file. Please help me... Thanks!!

Here is my .xml file (layout)

<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>

Here is my .java class codes

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

}

}

Thanks all again in advance!!! :D

Winona
  • 2,950
  • 2
  • 21
  • 29
  • Use android:onClick in the XML on the button and then make a function for the button click. Then you dont need that test for v==... – Anthony Graglia Jun 06 '11 at 15:52
  • 3
    See http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-android-app – PearsonArtPhoto Jun 06 '11 at 15:53
  • Thanks all for the quick response. -trgraglia, I think that's not the main problem because what I am trying to do now is to send the email capturing all the user input to a specific email address with the help of just a button. -Pearsonartphoto, thanks for the answer, however, is there a shorter way in doing that? Because I found that solution too cumbersome.. Sorry! – Winona Jun 06 '11 at 15:58

1 Answers1

2

There is no API to send an email through intents without user interaction. This has been repeated several times here in SO.

As pearsonartphoto suggested, you need to use an external library to send the email. JavaMail is the clear first option, even if you "found that solution too cumbersome".

So you need to gather all the fields as you posted, format the in a useful way for the email (perhaps xml, perhaps JSON?) and send it as text with JavaMail.

Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • or just send a post request to your own web server running perhaps PHP and use the `mail` function to send the mail out – james Jun 06 '11 at 17:13
  • Hi, thank you for your response. However, I'm still having doubts going about how to use JavaMail and using xml to gather the fields for the user. :/ Completely lost and heading to nowhere... – Winona Jun 07 '11 at 15:09
  • @Winona You can use JSON instead of XML for a simpler solution. Try the GSON or the org.json libraries. – Aleadam Jun 07 '11 at 21:53
  • Hi! I have successfully managed to send the email using JavaMail api. Thank you all for your response and answers!! :) I have also managed to use XML. :) – Winona Jun 08 '11 at 14:55
  • @Aleadam By using javaMail they are asking for from_mail_d and password ..How can i get that ? – hacker Mar 19 '12 at 08:28