Hi i have a list of numbers.
ArrayList<String> numbers;
I want to sent message to all these number using Intent, together .
I did this with email like sending email to multiple people , How to do for message ?
public static void send(Context ctx, String[] addy, String subject,
String body,File attachment) {
try {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("vnd.android-dir/mms-sms");
sendIntent.putExtra(android.content.Intent.EXTRA_PHONE_NUMBER,
addy);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
//sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
ctx.startActivity(Intent.createChooser(sendIntent,
"Send via which Application?"));
} catch (Exception e) {
Toast.makeText(ctx, "No activity was found to handle this action",
Toast.LENGTH_SHORT).show();
}
}
This is opening the message app but the numbers stored in addy are not listed in message sent to column
I did this with email like sending email to multiple people
Assuming that you used a third-party application via an Intent action, whether this works is up to the author of the third-party application. You have no way of knowing whether each of the hundreds of email apps for Android support specifying multiple addresses, unless you test them all.
How to do for message ?
You send them one at a time.
Your code is dreadful, using an undocumented MIME type that will not necessarily be honored on all devices, but EXTRA_PHONE_NUMBER is supposed to be a String, not a String[].
ACTION_SEND, or ACTION_SENDTO with an smsto: address, similarly is only guaranteed to support a single number and will give you the same problems as you have with sending email that way.
SmsManager has methods for directly sending SMS; they too only support one phone number at a time.
for java developer use
public static void send(Context ctx, String[] address, String subject,
String body, File attachment) {
List<String> mylist = Arrays.asList(address);
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
StringJoiner sj = new StringJoiner(";", "smsto:", "");
mylist.forEach(sj::add);
sendIntent.setData(Uri.parse(sj.toString()));
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
if(sendIntent.resolveActivity(ctx.getPackageManager()) != null) {
ctx.startActivity(sendIntent);
}
}
for those using kotlin, use
fun send(ctx: Context, address: Array<String>, subject: String, body: String, attachment: File) {
val sendIntent = Intent(Intent.ACTION_VIEW)
val data = address.toList().joinToString(";", "smsto:")
sendIntent.data = Uri.parse(data)
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject)
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body)
if(sendIntent.resolveActivity(ctx.packageManager) != null) {
ctx.startActivity(sendIntent)
}
}
Related
There is a part of my app where I send an email using a button but for some reason the Intent doesn't work and I don't understand why.
binding.IvMail.setOnClickListener {
val email = Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject))
.putExtra(Intent.EXTRA_TEXT, getString(R.string.email_text))
if (activity?.packageManager?.resolveActivity(email, 0) != null) {
startActivity(email)
}
}
I already searched for other ways to do it but everyone is using Intent.
// try this(JAVA).
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
////For(Kotlin)
sendEmail(recipient, subject, message)
private fun sendEmail(recipient: String, subject: String, message:
String) {
/*ACTION_SEND action to launch an email client installed on your Android device.*/
val mIntent = Intent(Intent.ACTION_SEND)
/*To send an email you need to specify mailto: as URI using
setData() method
and data type will be to text/plain using setType() method*/
mIntent.data = Uri.parse("mailto:")
mIntent.type = "text/plain"
// put recipient email in intent
/* recipient is put as array because you may wanna send email to multiple emails
so enter comma(,) separated emails, it will be stored array*/
mIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
//put the Subject in the intent
mIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
//put the message in the intent
mIntent.putExtra(Intent.EXTRA_TEXT, message)
try {
//start email intent
startActivity(Intent.createChooser(mIntent, "Choose Email Client..."))
}
catch (e: Exception){
//if any thing goes wrong for example no email client application or any exception
//get and show exception message
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
}
}
}
You have to use ACTION_SENDTO instead of ACTION_SEND.
See your new code below:
binding.IvMail.setOnClickListener {
val email = Intent(Intent.ACTION_SENDTO) // here you have to use SENDTO
.setType("text/plain")
.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject))
.putExtra(Intent.EXTRA_TEXT, getString(R.string.email_text))
if (activity?.packageManager?.resolveActivity(email, 0) != null) {
startActivity(email)
}
}
Let me know if not solved yet
Your code is fine. If it is not running, it would seem that your if statement is not satisfied and is skipping Intent resolvement.
What you can do to determine the problem is:
Debug if the code blocks are reached, by setting breakpoints and running debug app like this:
Log if the code blocks above are reached, by setting logs and looking for them in logcat tool like this:
You could also look in the Logcat for errors that happened when the intent was being ran.
I've written a function, for sending an email via intent. On my phone, gmail is installed, so the first part of this function is used... I want to send my database as a csv file, so it's quite a big string. If I send all data at once, NOTHING happens (no exception, no mail program that appears). If I split my data into two parts and send them one after another, it works. So there seems to be a limitation for the text.
Does anyone know what the limit is? Or is there another problem I'm not aware of?
public static void sendMailWithIntent(Activity activity, String subject, String text, boolean textIsHtml, String receiver)
{
try
{
Intent sendMailtoGmail = new Intent(Intent.ACTION_SEND);
sendMailtoGmail.setType("plain/text");
sendMailtoGmail.putExtra(Intent.EXTRA_EMAIL, new String[] {
receiver
});
sendMailtoGmail.putExtra(Intent.EXTRA_SUBJECT, subject);
sendMailtoGmail.putExtra(Intent.EXTRA_TEXT, textIsHtml ? Html.fromHtml(text) : text);
sendMailtoGmail.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
activity.startActivity(Intent.createChooser(sendMailtoGmail, ""));
}
catch (android.content.ActivityNotFoundException ex)
{
Intent sendGeneric = new Intent(Intent.ACTION_SEND);
sendGeneric.setType("plain/text");
sendGeneric.putExtra(Intent.EXTRA_EMAIL, new String[] {
receiver
});
sendGeneric.putExtra(Intent.EXTRA_SUBJECT, subject);
sendGeneric.putExtra(Intent.EXTRA_TEXT, textIsHtml ? Html.fromHtml(text) : text);
activity.startActivity(Intent.createChooser(sendGeneric, ""));
}
}
I am trying to use the android share intent from my application.
I have listed all the contacts from my contacts content provider in my application.
Now i want to send message to all the contacts i have selected(in my app) using any of the message app
installed in user phone.
I do not want to user smsmaanger , simply want to user any sms sending application in user mobile if
available.
I tried to do with email works great but not with sms .
I tried with email as works great
public static void send(Context ctx, String[] addy, String subject,
String body,File attachment) {
try {
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("message/rfc822");
sendIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
addy);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
//sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
ctx.startActivity(Intent.createChooser(sendIntent,
"Send via which Application?"));
} catch (Exception e) {
Toast.makeText(ctx, "No activity was found to handle this action",
Toast.LENGTH_SHORT).show();
}
}
For sms i am using like this .
public static void send(Context ctx, String addy, String subject,
String body,File attachment) {
try {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("vnd.android-dir/mms-sms");
sendIntent.putExtra(android.content.Intent.EXTRA_PHONE_NUMBER,
addy);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
//sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
ctx.startActivity(Intent.createChooser(sendIntent,
"Send via which Application?"));
} catch (Exception e) {
Toast.makeText(ctx, "No activity was found to handle this action",
Toast.LENGTH_SHORT).show();
}
}
I just simply want to add my all contacts to users message app for sending message, with a message
possible
To send SMS to multiple numbers you need to separate the numbers with ;
Sample here:
String toNumbers = "";
ArrayList<String> numbersArrayList;// your phone numbers here
for ( String number : numbersArrayList)
{
toNumbers = toNumbers + number + ";"//separating numbers with semicolon
}
toNumbers = toNumbers.subString(0, toNumbers.length - 1);// remove the last semicolon
...
sendIntent.putExtra(android.content.Intent.EXTRA_PHONE_NUMBER, toNumbers);
I am trying to use an intent to send an email from my application but the To field of the email will not populate. If I add code to fill in the subject or text, they work fine. Just the To field will not populate.
I have also tried changing the type to "text/plain" and "text/html" but I get the same problem. Can anyone help please?
public void Email(){
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822"); //set the email recipient
String recipient = getString(R.string.IntegralEmailAddress);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL , recipient);
//let the user choose what email client to use
startActivity(Intent.createChooser(emailIntent, "Send mail using...")); }
The email client I'm trying to use is Gmail
I think you are not passing recipient as array of string
it should be like
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "someone#gmail.com" });
In Kotlin - Android
fun sendMail(
activity: Activity,
emailIds: Array<String>,
subject: String,
textMessage: String
) {
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.type = "text/plain"
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailIds)
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
emailIntent.putExtra(Intent.EXTRA_TEXT, textMessage)
emailIntent.setType("message/rfc822")
try {
activity.startActivity(
Intent.createChooser(
emailIntent,
"Send email using..."
)
)
} catch (ex: ActivityNotFoundException) {
Toast.makeText(
activity,
"No email clients installed.",
Toast.LENGTH_SHORT
).show()
}
}
Also you can use [ val emailIntent = Intent(Intent.ACTION_SENDTO) ] to invoke direct email
client
//argument of function
val subject = "subject of you email"
val eMailMessageTxt = "Add Message here"
val eMailId1 = "emailId1#gmail.com"
val eMailId2 = "emailId2#gmail.com"
val eMailIds: Array<String> = arrayOf(eMailId1,eMailId2)
//Calling function
sendMail(this, eMailIds, subject, eMailMessageTxt)
I hope this code snippet will help to kotlin developers.
Use this
public void Email(){
// use this to declare your 'recipient' string and get your email recipient from your string xml file
Resources res = getResources();
String recipient = getString(R.string.IntegralEmailAddress);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822"); //set the email recipient
emailIntent.putExtra(Intent.EXTRA_EMAIL, recipient);
//let the user choose what email client to use
startActivity(Intent.createChooser(emailIntent, "Send mail using..."));
``}
This will work :)
This is what android documentation says about Intent.Extra_Email
-A string array of all "To" recipient email addresses.
So you should feed string properly
You can read more over here
http://developer.android.com/guide/components/intents-common.html#Email
and here http://developer.android.com/guide/topics/resources/string-resource.html Or use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Couple of things:
1 - You need to set the action constant variable as ACTION_SENDTO. Intent intentEmail = new Intent(Intent.ACTION_SENDTO);
2 - If you want it to be opened by only the mail then use the setData() method: intentEmail.setData(Uri.parse("mailto:")); Otherwise it will ask you to open it as text, image, audio file by other apps present on your device.
3 - You need to pass the email ID string as an array object and not just as a string. String is: "name#email.com". Array Object of the string is: new String[] {"email1", "email2", "more_email"}.
intentEmail.putExtra(Intent.EXTRA_EMAIL, new String[] {"email#overflow.com", "abcd#stack.com"});
private void callSendMeMail() {
Intent Email = new Intent(Intent.ACTION_SEND);
Email.setType("text/email");
Email.putExtra(Intent.EXTRA_EMAIL, new String[] { "me#gmail.com" });
Email.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
startActivity(Intent.createChooser(Email, "Send mail to Developer:"));
}
Made me waste so much time! Thanks to the accepted answer!
I'll add some Kotlin code with a couple of handy extension functions
fun Activity.hasEmailClient(): Boolean =
emailIntent("someAddress", "someSubject", "someText")
.resolveActivity(packageManager) != null
fun Activity.openEmailClient(address: String, subject: String, text: String) {
startActivity(emailIntent(address, subject, text))
}
private fun emailIntent(address: String, subject: String, text: String): Intent =
Intent(Intent.ACTION_SENDTO).apply {
type = "text/plain"
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf(address))
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, text)
}
feel free to replace Activity with Fragment if it suits your needs. Example usage:
if (hasEmailClient()) {
openEmailClient(
"example#email.info",
"this is the subject",
"this is the text"
)
}
This is what works for me:
val email = "recipient#email.com"
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:$email")
intent.putExtra(Intent.EXTRA_SUBJECT,"My Subject")
startActivity(intent)
First you should set the value of Intent.EXTRA_EMAIL as Array of Strings type like this:
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some#gmail.com" });
If this not works then simply uninstall the app and again Run....
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "xyx#gmail.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
My app integrates e-mail where the user can submit a bug report, feedback, etc. from the app directly. I'm using the application/octet-stream as the SetType for the Intent. When you go to submit the e-mail you get the content chooser and it shows various items from Evernote, Facebook, E-mail, etc.
How can I get this chooser to only show E-mail so as not to confuse the user with all these other items that fit the content chooser type?
Thank you.
To solve this issue simply follow the official documentation. The most important consideration are:
The flag is ACTION_SENDTO, and not ACTION_SEND.
The setData of method of the intent,
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
If you send an empty Extra, the if() at the end won't work and the app won't launch the email client.
This works for me. According to Android documentation. If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
https://developer.android.com/guide/components/intents-common.html#Email
I am presuming that you are using the ACTION_SEND Intent action, since you did not bother to actually state what you're using, but you agreed with #Aleadam's comment.
I'm using the application/octet-stream as the SetType for the Intent.
Nothing in that sentence limits things to email.
ACTION_SEND is a generic Intent action that can be supported by any application that wants to. All you do is indicate what data you are sharing and the MIME type of that data -- from there, it is up to the user to choose from available activities.
As #Jasoon indicates, you can try message/rfc822 as the MIME type. However, that is not indicating "only offer email clients" -- it indicates "offer anything that supports message/rfc822 data". That could readily include some application that are not email clients.
If you specifically want to send something by email, integrate JavaMail into your app, or write an email forwarding script on your Web server and invoke it, or something. If you use ACTION_SEND, you are implicitly stating that it is what the user wants that matters, and you want the user to be able to send such-and-so data by whatever means the user chooses.
Just struggled with this problem while implementing a Magic Link feature, a chooser intent for all installed email apps:
Chooser Intent Screenshot
private void openEmailApp() {
List<Intent> emailAppLauncherIntents = new ArrayList<>();
//Intent that only email apps can handle:
Intent emailAppIntent = new Intent(Intent.ACTION_SENDTO);
emailAppIntent.setData(Uri.parse("mailto:"));
emailAppIntent.putExtra(Intent.EXTRA_EMAIL, "");
emailAppIntent.putExtra(Intent.EXTRA_SUBJECT, "");
PackageManager packageManager = getPackageManager();
//All installed apps that can handle email intent:
List<ResolveInfo> emailApps = packageManager.queryIntentActivities(emailAppIntent, PackageManager.MATCH_ALL);
for (ResolveInfo resolveInfo : emailApps) {
String packageName = resolveInfo.activityInfo.packageName;
Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
emailAppLauncherIntents.add(launchIntent);
}
//Create chooser
Intent chooserIntent = Intent.createChooser(new Intent(), "Select email app:");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toArray(new Parcelable[emailAppLauncherIntents.size()]));
startActivity(chooserIntent);
}
There is a way more generic to do that, working with any MIME type.
See this post: How to customize share intent in Android?
It is possible to limit the choices of an intent chooser to just a few options. The code in the answer to this question is a good example. In essence, you would have to create a List of LabeledIntents to provide to the intent chooser, that will then include it in its list. Note that this solution works not on exclusion (certain apps are excluded while the rest remain) but instead you have to pick which apps to display. Hope it helps!
It works on all devices. It will show only Email Apps
public static void shareViaMail(Activity activity, String title, String body, String filePath) {
Uri URI = Uri.parse("file://" + filePath);
final Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"contact#brightsociety.com"});
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
try {
activity.startActivity(emailIntent);
} catch (Exception e) {
((BaseActivity) activity).showToast("Gmail App is not installed");
e.printStackTrace();
}
}
Kotlin Answer
If you need to show only email apps and then you want to open only inbox (not open new email writing), you need to do A and B:
A) Add below code in your AndroidManifest.xml file for Android 11 because of package visibility update of Android 11 :
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
<intent>
<action android:name="android.intent.action.CHOOSER" />
</intent>
</queries>
B) Use below function to show email chooser:
// Show email app list.
fun showEmailAppList() {
// Email app list.
val emailAppLauncherIntents: MutableList<Intent?> = ArrayList()
// Create intent which can handle only by email apps.
val emailAppIntent = Intent(Intent.ACTION_SENDTO)
emailAppIntent.data = Uri.parse("mailto:")
// Find from all installed apps that can handle email intent and check version.
val emailApps = packageManager.queryIntentActivities(
emailAppIntent,
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) 0 else PackageManager.MATCH_ALL
)
// Collect email apps and put in intent list.
for (resolveInfo in emailApps) {
val packageName = resolveInfo.activityInfo.packageName
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
emailAppLauncherIntents.add(launchIntent)
}
// Create chooser with created intent list to show email apps of device.
val chooserIntent = Intent.createChooser(Intent(), "OPEN EMAIL APP")
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toTypedArray())
startActivity(chooserIntent)
}
Result:
It works on all devices.It will show only Email Apps
public static void shareViaMail(Activity activity, String title, String body, String filePath) {
Uri URI = Uri.parse("file://" + filePath);
final Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"xyz#gmail.com"});
/*if you want to attach something*/
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
try {
activity.startActivity(emailIntent);
} catch (Exception e) {
((BaseActivity) activity).showToast("Gmail App is not installed");
e.printStackTrace();
}
}
Solution is very simple:
Intent testIntent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + "blah blah subject" + "&body=" + "blah blah body" + "&to=" + "sendme#me.com");
testIntent.setData(data);
startActivity(testIntent);
See: http://www.gaanza.com/blog/email-client-intent-android/
After a lot of searching and testing, I finally found a perfect solution. Thanks to the Open source developer, cketti for sharing his/her concise and neat solution.
String mailto = "mailto:bob#example.org" +
"?cc=" + "alice#example.com" +
"&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(bodyText);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));
try {
startActivity(emailIntent);
} catch (ActivityNotFoundException e) {
//TODO: Handle case where no email app is available
}
And this is the link to his/her gist.