MADPADGE
MADPADGE

Reputation: 113

Android Button onclick submit to email

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.

Upvotes: 4

Views: 7205

Answers (4)

Monirul Islam Prince
Monirul Islam Prince

Reputation: 25

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

Upvotes: 0

Thamays
Thamays

Reputation: 3098

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

Upvotes: 0

user766650
user766650

Reputation:

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[]{"[email protected]"});
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

Upvotes: 11

LuxuryMode
LuxuryMode

Reputation: 33741

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

Upvotes: 1

Related Questions