send Email through Android Intent - java

I have tried to send Email through Android Intent by using below code
Intent sendIntent = new Intent();
sendIntent.putExtra(Intent.EXTRA_TEXT, EmailContent);
sendIntent.putExtra(Intent.EXTRA_EMAIL, RecipientName );
sendIntent.putExtra(Intent.EXTRA_SUBJECT , subject );
sendIntent.setType("message/rfc822");
sendIntent.setAction(Intent.ACTION_SEND);
Intent chooser = Intent.createChooser(sendIntent , chooser_title );
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
In the email app the recipient details are not getting update whereas all the other details such as subject, body is getting updated with my input . Could you please suggest what needs to be done to resolve this .

Try to pass the EXTRA_EMAIL as string array.

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

Try using this:-
Create String resource for recipients:-
<string-array name="receipients">
<item>mgf#kgf.co</item>
<item>sdf#kgf.co</item>
</string-array>
and use this intent
Intent gmailIntent = new Intent(Intent.ACTION_SEND);
gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
gmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, resources.getStringArray(R.array.receipients));
gmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
gmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, EmailContent);
gmailIntent.setType("message/rfc822");
gmailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
final PackageManager pm = context.getApplicationContext().getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(gmailIntent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") ||
info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
if (best != null)
gmailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
try {
startActivity(gmailIntent);
} catch (ActivityNotFoundException ex) {
Toast.makeText(context.getApplicationContext(), getString(R.string.you_do_not_have_gmail_installed), Toast.LENGTH_SHORT).show();
}

Related

how to auto send message on whatsapp in android studio

how when clicked onclick button whatsapp messages can be sent automatically
public void onclick(){
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
Uri uri = Uri.parse("smsto:" + "628124291xxxx");
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "hallo");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
} else {
Toast.makeText(MainActivity.this, "WhatsApp not Installed",
Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
startActivity(goToMarket);
}
}
I don't know if it's the way you want it. I believe it is a way of doing. Try this.
String toNumber = "628124291xxxx";
String url = "https://api.whatsapp.com/send?phone="+ toNumber;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setPackage("com.whatsapp"); //com.whatsapp or com.whatsapp.w4b
i.setData(Uri.parse(url));
startActivity(i);

How do I fix "startActivity() in ContextCompat cannot be applied to?"

I am using Google Vision OCR to grab the email from a business card (the OCR Graphic activity) and send it to the the To destination in the SendEmail activity. My log shows that the email text is detected.
I tried to set the intent to send it to the next activity, but I am getting two errors, "cannot resolve constructor Intent" on my new intent, and start activity cannot be applied to.
This is the OcrGraphic activity
List<Line> lines = (List<Line>) text.getComponents();
for(Line elements : lines) {
float left = translateX(elements.getBoundingBox().left);
float bottom = translateY(elements.getBoundingBox().bottom);
if (elements != null && elements.getValue() != null) {
if (elements.getValue().matches("^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*#\"\n" +
"\t\t+ \"[A-Za-z0-9-]+(\\\\.[A-Za-z0-9]+)*(\\\\.[A-Za-z]{2,})$") || elements.getValue().contains("#")) {
Log.e("elementsemail", elements.getValue());
String email;
email = elements.getValue();
cEmail = email;
Intent sendIntent = new Intent(this, SendEmail.class);
sendIntent.putExtra(Intent.EXTRA_EMAIL, cEmail);
startActivity(sendIntent);
}
this is my Send Email activity
private void sendMail(){
Intent getIntent = getIntent();
String recipientList = getIntent.getStringExtra(OcrGraphic.cEmail);;
String[] recipients = recipientList.split(",");
String subject = mEditTextSubject.getText().toString();
String message = mEditTextMessage.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose an email client"));
}
I want to send the email address to the SendEmail activity. I am new to java and android, any help is welcomed.
I think you're problem is how you obtain the extra(EXTRA_EMAIL)
Replace String recipientList = getIntent.getStringExtra(OcrGraphic.cEmail);; with String recipientList = getIntent.getStringExtra(Intent.EXTRA_EMAIL);
please replace this :
Intent sendIntent = new Intent(this, SendEmail.class);
whit this :
Intent sendIntent = new Intent(getApplicationContext(), SendEmail.class);
edit
You need to pass context in a constructor like this
private Context context;
OcrGraphic(GraphicOverlay overlay, TextBlock text, Context context) {
super(overlay);
this.context = context;
}
And then
Intent sendIntent = new Intent(context, SendEmail.class);

how to share a File object by email?

I create a File object programmatically. I wonder how I can share it by email directly without saving it on the device.
I would like to have a method like this:
private void sendFileByEmail(File aFile, String emailTitle)
{
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, emailTitle);
// and then ???...
}
Thanks for your help !
This could be and option
private void sendFileByEmail(File aFile, String emailTitle)
{
Uri path = Uri.fromFile(afile);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd#gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
}

Android - getting an error “no application can perform this action” while trying to send a whatsapp message?

I am practising by making an app in which the user can send a WhatsApp message to a particular person .I tried some code snippets which i found on internet but whenever i try to send a WhatsApp msg from an actual device, i am gettig an error "No Application can perform this action".
Here is my code:-
public void sendMessage(View v) {
try
{
String whatsAppMessage = message.getText().toString();
Uri uri = Uri.parse("smsto:" + "9888873438");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
i.setType("text/plain");
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
}catch (Exception e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
}
}
Please help .
You receive no application can perform this action because you should remove i.setType("text/plain"); from your code:
String whatsAppMessage = message.getText().toString();
Uri uri = Uri.parse("smsto:" + "9888873438");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
Unfortunately as you can see WhatsApp now opens in the conversation activity but there isn't the text you set in the Intent. This is because WhatsApp doesn't support this kind of share. The only supported share with Intent is ACTION_SEND as you can see in the WhatsApp FAQ:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);

Restricting share options

Is there a way to restrict share options in an Android app? I have tried using the ShareActionProvider or just simply starting an intent using the Intent.ACTION_SEND intent option. Basically I want to be able to restrict sharing through email only or something of the sort.
you can use something like this, but instead of facebook look for a different name
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"this is a string");
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri); //Share the image on Facebook
PackageManager pm = getApplicationContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
startActivity(shareIntent);
break;
}
}
You can customize the intent chooser as per your need like this -
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject to be shared");
if (StringUtils.equals(packageName, "com.facebook.katana")){
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://link-to-be-shared.com");
}else{
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text message to shared");
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
Hope this helps you.

Categories