4

I have been working with Java and Xml for a few months now, and have learned a great deal thanks to everyones help on StackOverflow.

My question is about java programming for android in relation to the submit button.

Currently I am trying to figure out how to submit a value to an email address (behind the scenes)

Lets say we have a text field and a button; I want to take the value entered in the text field, and submit that to an email address onclick.

I am unable to find anything online that shows me how to do this.

Thank you in advance for reading through my post and I look forward to your suggestions.

MADPADGE
  • 113
  • 2
  • 12
  • if you want to use gmail, look at this post; http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-android-ap – aacanakin Jan 24 '12 at 21:40

4 Answers4

11

This is a great example of how using Intents can come in great handy! Android has a bunch of pre-defined Intents that do certain things within the system; you may have clicked on a picture before and a dialog popped up asking whether you would like to view it in your gallery or in a third-party app such as Astro. The viewing of an image has its own pre-determined intent.

Sending an email also has its own pre-determined intent: android.content.Intent.ACTION_SEND. You'll need to create an intent with that property and then attach extra information (ie. the address to send to, the subject/message body, etc.).

Example code:

// Data members
private Intent emailIntent;
private String feedback;
private EditText feedbackBox;

// Create the Intent, and give it the pre-defined value
// that the Android machine automatically associates with
// sending an email.
emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");

// Put extra information into the Intent, including the email address
// that you wish to send to, and any subject (optional, of course).
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"your_email@whatever.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Insert subject here");

// Acquire feedback from an EditText and save it to a String.
feedback = feedbackBox.getText().toString();

// Put the message into the Intent as more extra information,                   
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, feedback);

// Start the Intent, which will launch the user's email 
// app (make sure you save any necessary information in YOUR app
// in your onPause() method, as launching the email Intent will
// pause your app). This will create what I discussed above - a
// popup box that the user can use to determine which app they would like
// to use in order to send the email.
startActivity(Intent.createChooser(emailIntent, "Insert title for dialog box."));

I hoped this helped!!

Some sources you might like to check out:
http://developer.android.com/guide/topics/intents/intents-filters.html
http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND

1
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, someEditText.getText());
startActivity(Intent.createChooser(emailIntent, "Send someone an email..."));
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
0

try this

Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:Type email address here"));
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
Thamays
  • 2,978
  • 26
  • 31
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". – abarisone Jul 20 '16 at 11:11
0

It works good

 String   mail=mailid.getText().toString();
 String   msubject=subject.getText().toString();
 String   mbody=body.getText().toString();
    Log.i("Send email", "");
    String[] TO = {mail};
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Static subject "+ msubject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Static body "+ mbody);

    try {
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        finish();

    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
    }