Link in email sent with Intent - android

In my app I have it so that the user can send an email (code below).
This works, but I would like to improve it. There are 2 possibilities:
1. Currently, the url appears as a String. Can it be made to appear as a link in the email?
2. Include an image in the email (mylogo.png) and clicking on the image would go to the url
Is either of these possible?
private void sendEmail() {
final UserInfo userInfo = UserInfo.getInstance();
final String highScore = userInfo.getCumulativeScore();
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
//intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(crashLogFile)); // attach a file
// EXTRA_EMAIL is the recipient, which in this case we don't know, so leave blank and let user fill in
//intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"Extra Email"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Super Quiz High Score");
String url = "https://play.google.com/store/apps/details?id=com.devname.appname";
intent.putExtra(Intent.EXTRA_TEXT, "My high score in the Super Quiz is now " + highScore + "!\n\n" + url);
startActivity(createEmailOnlyChooserIntent(intent, "Send via email"));
}

Depends completely on the mail client that the receiver uses. If it's gmail, you're out of luck, they don't parse links.

Related

How to filter the apps that show up when sharing intent Android?

I know this has been asked before and I reference one of the answers but I can't get my code to work the way it should. I have a simple note-taking app, which allows the user to share notes through external applications. The code is supposed to filter the apps so the user can only share to facebook, twitter, mms, gmail, and email (so I thought). I took it from the top answer here How to filter specific apps for ACTION_SEND intent (and set a different text for each app). When I click on the button that launches the method to share intents, I do get mms, gmail, facebook twitter, and email, but I also get google drive, android beam, evernote, twitter direct message, facebook messenger and snapchat.
The only apps that the information gets sent correctly to are mms, email and gmail. Facebook doesn't work, there's some comments below on why, and twitter I'm not sure of because I don't have an account to test it out. I don't have the code to check if the packageName contains "drive" or "googledrive", but I can still share to drive and the data from my note gets passed along. I want to be able to send the note text and the note title however (2 editText fields in my app), but I don't know how to since I don't know what the package name is.
For the apps that don't work or I want to remove from the list of choices, how can I get rid of them? Here is the code:
public void sendNote(View view) {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
emailIntent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString());
emailIntent.putExtra(Intent.EXTRA_SUBJECT, noteEditor.getText().toString());
emailIntent.setType("message/rfc822");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());
} else if(packageName.contains("facebook")) {
// Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
// One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
// will show the <meta content ="..."> text from that page with our link in Facebook.
intent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());
} else if(packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
intent.putExtra(Intent.EXTRA_TEXT, noteEditor.getText().toString());
intent.putExtra(Intent.EXTRA_SUBJECT, titleEditor.getText().toString());
intent.setType("message/rfc822");
}
//else if (packageName.contains("drive")) {
//intent.putExtra(Intent.EXTRA_TEXT, titleEditor.getText().toString() + ": " + noteEditor.getText().toString());
//}
else {
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}
I have an else if near the bottom that is commented out. That was my attempt in finding the google drive packageName and sending the intent so the user can still use that application, it didn't work. Like I said earlier, drive gets information sent to it, but it's just the note text, not the title and text like I want it to be.
So I have 2 questions.
How can I filter out all of the apps that I don't want the user to be able to send information to so they don't even show up as an option, lets take twitter for example.
If the above is not possible, how can I find the package names for these different apps? For Google Drive, I tried using the commented out else if statement to send an intent to the app there but it never executed. I've searched everywhere to get intents for google drive but I can't get the package name to get my code working.
Thanks in advance

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.

Pre-populated recipient in Android email?

I am making an application where the user will click on contact us section and it will open the built-in Gmail application and I have the code to pre-populate the subject and message body in the mail.
I want the code to pre-populate the "To: " section where we add the recipient.
That didn't work for me but this did
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?to="+emailaddress+"&subject=" + subject + "&body=" + body);
intent.setData(data);
startActivity(intent);
Hope this helps someone else out there
In the intent you use to populate the subject and message, add the following:
intent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "youremail#domain.com" });
Check the documentation for EXTRA_EMAIL

Android email replaces space with plus(+)

I am sending an email in action view, it works perfectly fine in gmail , but if the user chooses any other mailing service it replaces spaces with '+'
like in body text is "check out it is a good day"
it displays as "check+out+it+is+a+good+day"
Any idea how to solve this issues
Here is my function for sending email
private void sendToAFriend() {
String subject = "it is a good day ";
String body = "Check out it is a good day";
String uriText =
"mailto:" +
"?subject=" + URLEncoder.encode(subject) +
"&body=" + URLEncoder.encode(body);
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
startActivity(Intent.createChooser(sendIntent, "Send email"));
}
Try this code.
Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");
intent.putExtra(Intent.EXTRA_TEXT, "Body of email");
intent.setData(Uri.parse("mailto:default#recipient.com")); // or just "mailto:" for blank
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
startActivity(intent);
From the description of the method URLEncoder.encode
java.net.URLEncoder.encode(String s)
Deprecated. use encode(String, String) instead.
Encodes a given string s in a x-www-form-urlencoded string using the specified encoding scheme enc.
All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9') and characters '.', '-', '*', '_' are converted into their hexadecimal value prepended by '%'. For example: '#' -> %23. In addition, spaces are substituted by '+'
Use Uri.encode(String) instead of the URLEncoder, it handles spaces correctly for this use case.
ACTION_VIEW with mailto link is more preferable if you wish to limit the sending options to email only.
Just use without any encode.
"&body=" + body;
it works for me!

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

Categories

Resources