Android Button onclick submit to email - android

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.

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

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

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

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

Related

Sending data with multiple attachments with email intent

I have a FeedbackActivity.java activity which takes feedback from user with multiple attachments (upto 3 images as attachments).
I am using following code:
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, emails); //emails is an Array of 'String' type
intent.putExtra(Intent.EXTRA_SUBJECT, subject); //subject is a String
intent.putExtra(Intent.EXTRA_TEXT, text) //text is a String
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); //uris is an ArrayList of 'Uri' type
//uris stores all Uri of images selected
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
else {
Toast.makeText(this, "Not Good", Toast.LENGTH_SHORT).show();
}
Now this code works fine but the problem is that it shows all sorts of apps which support "message/rfc822" MIME type.
Image is shown below :
I only need to show the email client apps, I tried Uri.parse("mailto:"), but didn't workout and code always moves to else statement and shows the toast "not good".
I read the google documentation but it only shows simple cases.
I tried searching on the web. Many developers are using intent.setType("*/*") or intent.setType("text/plain"). But they all too show apps other than email clients.
Please guide me.
And I wanted to ask in general,
Google documentations show simple examples which is good in a way, but how to learn really in depth on these kind of topics?
Thank you.
So here, we will be using two intents: selectorIntent and emailIntent. selectorIntent is what the emailIntent will use as to show available apps. code:
Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emails);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
emailIntent.setSelector(selectorIntent);
if(emailIntent.resolveActivity(getPackageManager()) != null){
startActivity(emailIntent);
}
else {
Snackbar.make(scrollView, "Sorry, We couldn't find any email client apps!", Snackbar.LENGTH_SHORT).show();
}
Now it will choose only apps which are email client.
If there is only one email-client app in your phone than it will directly open that. And if no such application is there, than the code will show Snackbar given in the else part.
Don't use Uri.parse, use Uri.fromParts
Do it like this:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","example#mail.com", null));

Send email intent with image in html body

Is it possible to send an email, using an intent, with an image in the body.
I'm using an image that is hosted...
public void sendEmail(View view){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Image in body test");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(getHtmlBody()));
startActivity(Intent.createChooser(intent, "Send Email"));
}
private String getHtmlBody() {
String html = "<h1><img width=\"100\" src=\"http://cdn2-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-8.jpg\"> Hello World </h1>";
return html;
}
I am able to do this using javaMail but that sends the email automatically without the user being able to see anything so I'm hoping I can use intents.
There is EXTRA_HTML_TEXT that allows you to supply HTML as alternative to the text in EXTRA_TEXT. However, there's no guarantee that the receiving app supports this extra (hence the requirement that EXTRA_TEXT must be present, too).
The code could look something like this:
public void sendEmail(View view){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Image in body test");
intent.putExtra(Intent.EXTRA_TEXT, getTextBody());
intent.putExtra(Intent.EXTRA_HTML_TEXT, getHtmlBody());
startActivity(Intent.createChooser(intent, "Send Email"));
}
private String getHtmlBody() {
return "<h1><img width=\"100\" src=\"http://cdn2-www.dogtime.com/assets/uploads/gallery/30-impossibly-cute-puppies/impossibly-cute-puppy-8.jpg\">"
+ getTextBody() + "</h1>";
}
private String getTextBody() {
return "Hello world";
}
Apps that don't support HTML will simply use the text from EXTRA_TEXT.
The answer by Arpit Garg in this post shares that and tags do not work in most email clients, so unfortunately your code wont work. I just tested your code with the Gmail app and a few others and it pulled in a placeholder but not the actual image. what you can do though is add an image as an attachment.

Send link to Whatsapp via Android Intent

I'm trying to send a text message with a link from my android app to chat applications like Whatsapp or SMS message.
These apps don't accept text/html type as an Intent type and when I'm using text/plain type my message is being sent with the subject only and without the message's body.
I've seen apps that can share links via Whatsapp like Chrome and Dolphin Browser apps.
Here is my code:
#JavascriptInterface
public void sendMessage(String trip) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Trip from Voyajo");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("I've found a trip in Voyajo website that might be interested you, http://www.voyajo.com/viewTrip.aspx?trip=" + trip));
startActivity(Intent.createChooser(emailIntent, "Send to friend"));
}
#JavascriptInterface
public void sendMessage(String trip) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Trip from Voyajo");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("I've found a trip in Voyajo website that might be interested you, http://www.voyajo.com/viewTrip.aspx?trip=" + trip));
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Send to friend"));
}
here i just change position of emailIntent.setType("text/plain"); this line and it works.
you get your link in messaging app body email app body.but here you can get subject text only in Mail apps not in messaging app but you can get your link in body so achive your goal...
Thats it...

How to send a picture via email in Android, previewed but NOT attached..?

I want to send a picture preview in my mail Intent.
I dont want to attach it, i just want it to be shown.
This is my intent:
String textToSend = getString(R.string.mailHi)+"<br><br>"+getString(R.string.mailText)+getTextToSendViaMail();
Uri pngUri = null;
File currentShopImage = new File(Environment.getExternalStorageDirectory().toString()+"/OpenGuide/"+Integer.toString(keyId)+"_1_normal.pn_");
if(currentShopImage.exists()){
File pngFile = new File(currentShopImage.toString());
pngUri = Uri.fromFile(pngFile);
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
//i.putExtra(Intent.EXTRA_EMAIL, new String[] { emailCim });
i.putExtra(Intent.EXTRA_SUBJECT, "OpenGuide");
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(textToSend));
if(pngUri!= null)
i.putExtra(Intent.EXTRA_STREAM, pngUri);
try {
startActivity(Intent.createChooser(i, getString(R.string.SendMail)));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ShopActivity.this, getString(R.string.MailClientNotFound), Toast.LENGTH_SHORT).show();
}
How can i achive such a thing ?
As far as I understood your problem, you want to place the image inside the mail with other text. In the words of Email protocol it's called an INLINE Attachment.
You would not be able to do this with intents as none of the email clients installed on the device supports creating html messages.
If it's really a core part of your app, you should consider a third party api to do this. One of the such library is JavaMail. You would be able to send html messages through this library but will take some time in setting up.
Here's a link that may give you some hint, how it's done.
Then you need to send a HTML generated email afaik.

Messaging and email intents in Android?

I've searched Google for this, but have only found similar examples--not exactly what I need. I simply need to start messaging (SMS) and email intents from my app with their "to" fields already populated. So I need to send a number with the sms intent and an email address with the email intent. Any help would be appreciated.
For the e-mail part :
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"foo#bar.com"});
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Send a mail ..."));
from Only Email apps to resolve an Intent
String recepientEmail = ""; // either set to destination email or leave empty
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + recepientEmail));
startActivity(intent);
will filter out all non-email applications.

Categories

Resources