Android: Send information from App [duplicate] - java

I am developing an application in Android. I don't know how to send an email from the application?

The best (and easiest) way is to use an Intent:
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();
}
Otherwise you'll have to write your own client.

Use .setType("message/rfc822") or the chooser will show you all of the (many) applications that support the send intent.

I've been using this since long time ago and it seems good, no non-email apps showing up. Just another way to send a send email intent:
Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");
intent.putExtra(Intent.EXTRA_TEXT, "Body of email");
intent.setData(Uri.parse("mailto:default#example.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);

I was using something along the lines of the currently accepted answer in order to send emails with an attached binary error log file. GMail and K-9 send it just fine and it also arrives fine on my mail server. The only problem was my mail client of choice Thunderbird which had troubles with opening / saving the attached log file. In fact it simply didn't save the file at all without complaining.
I took a look at one of these mail's source codes and noticed that the log file attachment had (understandably) the mime type message/rfc822. Of course that attachment is not an attached email. But Thunderbird cannot cope with that tiny error gracefully. So that was kind of a bummer.
After a bit of research and experimenting I came up with the following solution:
public Intent createEmailOnlyChooserIntent(Intent source,
CharSequence chooserTitle) {
Stack<Intent> intents = new Stack<Intent>();
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
"info#example.com", null));
List<ResolveInfo> activities = getPackageManager()
.queryIntentActivities(i, 0);
for(ResolveInfo ri : activities) {
Intent target = new Intent(source);
target.setPackage(ri.activityInfo.packageName);
intents.add(target);
}
if(!intents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(intents.remove(0),
chooserTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
intents.toArray(new Parcelable[intents.size()]));
return chooserIntent;
} else {
return Intent.createChooser(source, chooserTitle);
}
}
It can be used as follows:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("*/*");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(crashLogFile));
i.putExtra(Intent.EXTRA_EMAIL, new String[] {
ANDROID_SUPPORT_EMAIL
});
i.putExtra(Intent.EXTRA_SUBJECT, "Crash report");
i.putExtra(Intent.EXTRA_TEXT, "Some crash report details");
startActivity(createEmailOnlyChooserIntent(i, "Send via email"));
As you can see, the createEmailOnlyChooserIntent method can be easily fed with the correct intent and the correct mime type.
It then goes through the list of available activities that respond to an ACTION_SENDTO mailto protocol intent (which are email apps only) and constructs a chooser based on that list of activities and the original ACTION_SEND intent with the correct mime type.
Another advantage is that Skype is not listed anymore (which happens to respond to the rfc822 mime type).

To JUST LET EMAIL APPS to resolve your intent you need to specify ACTION_SENDTO as Action and mailto as Data.
private void sendEmail(){
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:" + "recipient#example.com")); // You can use "mailto:" if you don't know the address beforehand.
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My email's subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "My email's body");
try {
startActivity(Intent.createChooser(emailIntent, "Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Activity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
}
}

The solution to this is simple: the android documentation explains it.
(https://developer.android.com/guide/components/intents-common.html#Email)
The most important is the flag: it is ACTION_SENDTO, and not ACTION_SEND
The other important line is
intent.setData(Uri.parse("mailto:")); ***// only email apps should handle this***
By the way, if you send an empty Extra, the if() at the end won't work and the app won't launch the email client.
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);
}
}

The strategy of using .setType("message/rfc822") or ACTION_SEND seems to also match apps that aren't email clients, such as Android Beam and Bluetooth.
Using ACTION_SENDTO and a mailto: URI seems to work perfectly, and is recommended in the developer documentation. However, if you do this on the official emulators and there aren't any email accounts set up (or there aren't any mail clients), you get the following error:
Unsupported action
That action is not currently supported.
As shown below:
It turns out that the emulators resolve the intent to an activity called com.android.fallback.Fallback, which displays the above message. Apparently this is by design.
If you want your app to circumvent this so it also works correctly on the official emulators, you can check for it before trying to send the email:
private void sendEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO)
.setData(new Uri.Builder().scheme("mailto").build())
.putExtra(Intent.EXTRA_EMAIL, new String[]{ "John Smith <johnsmith#yourdomain.com>" })
.putExtra(Intent.EXTRA_SUBJECT, "Email subject")
.putExtra(Intent.EXTRA_TEXT, "Email body")
;
ComponentName emailApp = intent.resolveActivity(getPackageManager());
ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
if (emailApp != null && !emailApp.equals(unsupportedAction))
try {
// Needed to customise the chooser dialog title since it might default to "Share with"
// Note that the chooser will still be skipped if only one app is matched
Intent chooser = Intent.createChooser(intent, "Send email with");
startActivity(chooser);
return;
}
catch (ActivityNotFoundException ignored) {
}
Toast
.makeText(this, "Couldn't find an email app and account", Toast.LENGTH_LONG)
.show();
}
Find more info in the developer documentation.

Sending email can be done with Intents which will require no configuration. But then it will require user interaction and the layout will be a bit restricted.
Build and sending a more complex email without user interaction entails building your own client. The first thing is that the Sun Java API for email are unavailable. I have had success leveraging the Apache Mime4j library to build email. All based on the docs at nilvec.

Here is the sample working code which opens mail application in android device and auto-filled with To address and Subject in the composing mail.
protected void sendEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:feedback#gmail.com"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}

I use the below code in my apps. This shows exactly email client apps, such as Gmail.
Intent contactIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", getString(R.string.email_to), null));
contactIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject));
startActivity(Intent.createChooser(contactIntent, getString(R.string.email_chooser)));

This will show you only the email clients (as well as PayPal for some unknown reason)
public void composeEmail() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hi#example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
try {
startActivity(Intent.createChooser(intent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}

This is how I did it. Nice and simple.
String emailUrl = "mailto:email#example.com?subject=Subject Text&body=Body Text";
Intent request = new Intent(Intent.ACTION_VIEW);
request.setData(Uri.parse(emailUrl));
startActivity(request);

I used this code to send mail by launching default mail app compose section directly.
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setType("message/rfc822");
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"test#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

This function first direct intent gmail for sending email, if gmail is not found then promote intent chooser. I used this function in many commercial app and it's working fine. Hope it will help you:
public static void sentEmail(Context mContext, String[] addresses, String subject, String body) {
try {
Intent sendIntentGmail = new Intent(Intent.ACTION_VIEW);
sendIntentGmail.setType("plain/text");
sendIntentGmail.setData(Uri.parse(TextUtils.join(",", addresses)));
sendIntentGmail.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
sendIntentGmail.putExtra(Intent.EXTRA_EMAIL, addresses);
if (subject != null) sendIntentGmail.putExtra(Intent.EXTRA_SUBJECT, subject);
if (body != null) sendIntentGmail.putExtra(Intent.EXTRA_TEXT, body);
mContext.startActivity(sendIntentGmail);
} catch (Exception e) {
//When Gmail App is not installed or disable
Intent sendIntentIfGmailFail = new Intent(Intent.ACTION_SEND);
sendIntentIfGmailFail.setType("*/*");
sendIntentIfGmailFail.putExtra(Intent.EXTRA_EMAIL, addresses);
if (subject != null) sendIntentIfGmailFail.putExtra(Intent.EXTRA_SUBJECT, subject);
if (body != null) sendIntentIfGmailFail.putExtra(Intent.EXTRA_TEXT, body);
if (sendIntentIfGmailFail.resolveActivity(mContext.getPackageManager()) != null) {
mContext.startActivity(sendIntentIfGmailFail);
}
}
}

This is the most clean way of sending email on Android.
val intent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf("email#example.com"))
putExtra(Intent.EXTRA_SUBJECT, "Subject")
putExtra(Intent.EXTRA_TEXT, "Email body")
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
You also need to specify in your manifest (outside your application tag) the query for applications that handle email (mailto)
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>
If you need to send HTML text in the email body, please replace the "Email body" with your email string, something like this (please beware that Html.fromHtml maybe deprecated this was only for show you how to do it)
Html.fromHtml(
StringBuilder().append("<b>Hello world</b>").toString()
)

simple try this one
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextTo);
textSubject = (EditText) findViewById(R.id.editTextSubject);
textMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
String message = textMessage.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
// email.putExtra(Intent.EXTRA_CC, new String[]{ to});
// email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
// need this to prompts email client only
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
});
}

Other solution can be
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.setType("plain/text");
emailIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"someone#gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Yo");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hi");
startActivity(emailIntent);
Assuming most of the android device has GMail app already installed.

Use this for send email...
boolean success = EmailIntentBuilder.from(activity)
.to("support#example.org")
.cc("developer#example.org")
.subject("Error report")
.body(buildErrorReport())
.start();
use build gradle :
compile 'de.cketti.mailto:email-intent-builder:1.0.0'

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","ebgsoldier#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Forgot Password");
emailIntent.putExtra(Intent.EXTRA_TEXT, "this is a text ");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

This method work for me. It open Gmail app (if installed) and set mailto.
public void openGmail(Activity activity) {
Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setType("text/plain");
emailIntent.setType("message/rfc822");
emailIntent.setData(Uri.parse("mailto:"+activity.getString(R.string.mail_to)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, activity.getString(R.string.app_name) + " - info ");
final PackageManager pm = activity.getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 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)
emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
activity.startActivity(emailIntent);
}

/**
* Will start the chosen Email app
*
* #param context current component context.
* #param emails Emails you would like to send to.
* #param subject The subject that will be used in the Email app.
* #param forceGmail True - if you want to open Gmail app, False otherwise. If the Gmail
* app is not installed on this device a chooser will be shown.
*/
public static void sendEmail(Context context, String[] emails, String subject, boolean forceGmail) {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL, emails);
i.putExtra(Intent.EXTRA_SUBJECT, subject);
if (forceGmail && isPackageInstalled(context, "com.google.android.gm")) {
i.setPackage("com.google.android.gm");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else {
try {
context.startActivity(Intent.createChooser(i, "Send mail..."));
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No email app is installed on your device...", Toast.LENGTH_SHORT).show();
}
}
}
/**
* Check if the given app is installed on this devuice.
*
* #param context current component context.
* #param packageName The package name you would like to check.
* #return True if this package exist, otherwise False.
*/
public static boolean isPackageInstalled(#NonNull Context context, #NonNull String packageName) {
PackageManager pm = context.getPackageManager();
if (pm != null) {
try {
pm.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return false;
}

Try this:
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
}
The above code will open the users favourite email client prefilled with the email ready to send.
Source

The code below works on Android 10 devices and higher. It also sets the subject, body and recipient(To).
val uri = Uri.parse("mailto:$EMAIL")
.buildUpon()
.appendQueryParameter("subject", "App Feedback")
.appendQueryParameter("body", "Body Text")
.appendQueryParameter("to", EMAIL)
.build()
val emailIntent = Intent(Intent.ACTION_SENDTO, uri)
startActivity(Intent.createChooser(emailIntent, "Select app"))

Kotlin version which only shows Email clients (no contacts etc.):
with(Intent(Intent.ACTION_SEND)) {
type = "message/rfc822"
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf("user#example.com"))
putExtra(Intent.EXTRA_SUBJECT,"YOUR SUBJECT")
putExtra(Intent.EXTRA_TEXT, "YOUR BODY")
try {
startActivity(Intent.createChooser(this, "Send Email with"))
} catch (ex: ActivityNotFoundException) {
// No email clients found, might show Toast here
}
}

import androidx.core.app.ShareCompat
import androidx.core.content.IntentCompat
ShareCompat.IntentBuilder(this)
.setType("message/rfc822")
.setEmailTo(arrayOf(email))
.setStream(uri)
.setSubject(subject)
.setText(message + emailMessage)
.startChooser()

Filtering for 'real' E-Mail Apps is still an issue today. As many people mentioned above, other apps nowadays also report to support the mime-type "message/rfc822". Therefore, this mime-type is not suitable any more to filter for a real E-Mail App.
If you want to send a simple text mail, it is enough to use the the ACTION_SENDTO intent action with the appropriate data type like so:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, text);
Intent chooser = Intent.createChooser(intent, "Send Mail");
context.startActivity(chooser);
This will filter all available apps for those that support the 'mailto' protocol, which is much more suitable for the purpose of sending an E-mail.
But sadly things become complicated, if you want to send a mail with (multiple) attachments. The ACTION_SENDTO action does not support the EXTRA_STREAM extra on the intent. If you want to use that, you must use the ACTION_SEND_MULTIPLE action, which does not work together with the the data type Uri.parse("mailto:").
For now I found a solution, which consists of the following steps:
Declare that your App wants to query Apps on the device, that support the mailto protocol (important for all Apps since Android 11)
Actually query all Apps, that support the mailto protocol
For each supporting App: Build the intent that you actually want to launch, aiming for that single App
Build the App chooser and launch it
And this is how it looks in code:
Add this to the AndroidManifest:
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>
This is the Java code:
/* Query all Apps that support the 'mailto' protocol */
PackageManager pm = context.getPackageManager();
Intent emailCheckerIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
List<ResolveInfo> emailApps = pm.queryIntentActivities(emailCheckerIntent, PackageManager.MATCH_DEFAULT_ONLY);
/* For each supporting App: Build an intent with the desired values */
List<Intent> intentList = new ArrayList<>();
for (ResolveInfo resolveInfo : emailApps) {
String packageName = resolveInfo.activityInfo.packageName;
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setPackage(packageName);
intent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name));
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, text);
intent.putExtra(Intent.EXTRA_STREAM, attachmentUris);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //IMPORTANT to give the E-Mail App access to your attached files
intentList.add(intent);
}
/* Create a chooser consisting of the queried apps only */
Intent chooser = Intent.createChooser(intentList.remove(intentList.size() - 1), "Send Mail");
Intent[] extraIntents = intentList.toArray(new Intent[0]);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
context.startActivity(chooser);
Note: If the itentList only has one item, Android will automatically skip the chooser and run the only App automatically.

Related

I cant run the email intent code on my phone

enter image description here
the email intent code is completely run in the android mobile model provided by android studio but when I run the code in my personal phone its not work…
please answer ??
Don't forget to set the type of intent so it will trigger email clients
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_TEXT, message);
//need this to prompts email client only
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
Kotlin code,
val selectorIntent = Intent(Intent.ACTION_SENDTO)
selectorIntent.data = Uri.parse("mailto:")
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf<String>("mail id"))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject")
emailIntent.selector = selectorIntent
startActivity(Intent.createChooser(emailIntent, "Send email..."))
Java code,
Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"mail id"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.setSelector(selectorIntent);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
Intent intEmail = new Intent(Intent.ACTION_SENDTO);
intEmail.setType("plain/text");
intEmail.setData(Uri.parse("mailto:"));
intEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{"receiver_email_address"});
if (intEmail.resolveActivity(getPackageManager()) != null){
startActivity(intEmail);
}
I tested the code of mayar hassan, it worked fine on my side. [on Samsung Android 8.1]
The code:
public class TestSentMail extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_sent_mail);
Button button = findViewById(R.id.submitButton);
button.setOnClickListener(view -> {
submitOrder();
});
}
private void submitOrder() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "coffe order for tancolo");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
The screenshot:
How to fix your problem
In general, I think we should do these as below.
Check the log in the Logcat panel in Android Studio, maybe there are exceptions.
Added your debug code where you want to add.
val i = Intent(Intent.ACTION_SEND)
i.type = "message/rfc822"
i.putExtra(Intent.EXTRA_EMAIL, arrayOf<String>("sumsolutions.net#gmail.com"))
i.putExtra(Intent.EXTRA_SUBJECT, "Feedback")
i.putExtra(Intent.EXTRA_TEXT, "Text here...")
try {
startActivity(Intent.createChooser(i, "Send mail..."))
} catch (ex: ActivityNotFoundException) {
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT)
.show()
}

How to open gmail in android

I just wanted to open the Gmail app through my app and wanted to set email, subject and message from my application.
I have tried GmailService but it is not supporting bcc or cc emails.
Link: https://github.com/yesidlazaro/GmailBackground
BackgroundMail.newBuilder(this)
.withUsername("username#gmail.com")
.withPassword("password12345")
.withMailto("toemail#gmail.com")
.withType(BackgroundMail.TYPE_PLAIN)
.withSubject("this is the subject")
.withBody("this is the body")
.withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() {
#Override
public void onSuccess() {
//do some magic
}
}).withOnFailCallback(new BackgroundMail.OnFailCallback() {
#Override
public void onFail() {
//do some magic
}
}).send();
I would like to use bcc and cc functionality along with the attachment, subject, and message.
// For Email by Any app
Intent email= new Intent(Intent.ACTION_SENDTO);
email.setData(Uri.parse("mailto:your.email#gmail.com"));
email.putExtra(Intent.EXTRA_SUBJECT, "Subject");
email.putExtra(Intent.EXTRA_TEXT, "My Email message");
startActivity(email);
open gmail via Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("abc#gmail.com"));
intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
intent.putExtra(Intent.EXTRA_CC, new String[]{"xyz#gmail.com"});
intent.putExtra(Intent.EXTRA_BCC, new String[]{"pqr#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "your subject goes here...");
intent.putExtra(Intent.EXTRA_TEXT, "Your message content goes here...");
startActivity(intent);
just pass EXTRA_CC & EXTRA_BCC in intent argument
Edit
Below answer will work on android 11
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Your subject here...");
intent.putExtra(Intent.EXTRA_TEXT,"Your message here...");
startActivity(intent);
Edit 2
val selectorIntent = Intent(Intent.ACTION_SENDTO)
selectorIntent.data = Uri.parse("mailto:")
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("recipient#mail.com"))
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject here...")
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email Body...")
emailIntent.selector = selectorIntent
activity!!.startActivity(Intent.createChooser(emailIntent, "Send email..."))
// This is for Gmail App
Intent email= new Intent(Intent.ACTION_VIEW);
email.setType("message/rfc822")
.setData(Uri.parse("mailto:your.email#gmail.com"))
.putExtra(Intent.EXTRA_EMAIL, "your.email#gmail.com")
.putExtra(Intent.EXTRA_SUBJECT, "Subject")
.putExtra(Intent.EXTRA_TEXT, "My Email message")
.setPackage("com.google.android.gm");
startActivity(email);
I m using this to launch gmail app.
val intent: Intent? = activity.packageManager.getLaunchIntentForPackage("com.google.android.gm")
if (intent != null) {
startActivity(intent)
}
else{
showToast("Sorry...You don't have gmail app")
}
//This is open with gmail
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setType("text/plain");
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Mail Subject");
i.putExtra(Intent.EXTRA_TEXT , "massage");
i.setPackage("com.google.android.gm");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(AnotherActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
I am using this
Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
startActivity(mailClient);
you can also try this
final Intent intent = new Intent(Intent.ACTION_VIEW)
.setType("plain/text")
.setData(Uri.parse("test#gmail.com"))
.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail")
.putExtra(Intent.EXTRA_EMAIL, new String[]{"test#gmail.com"})
.putExtra(Intent.EXTRA_SUBJECT, "test")
.putExtra(Intent.EXTRA_TEXT, "hello. this is a message sent from my demo app :-)");
startActivity(intent);
use for plenty of emails:
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test#gmail.com" });
for single emails:
intent.setData(Uri.parse("test#gmail.com"));

How can I send message to specific contact through WhatsApp from my android app?

I am developing an android app and I need to send a message to specific contact from WhatsApp.
I tried this code:
Uri mUri = Uri.parse("smsto:+999999999");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat",true);
startActivity(mIntent);
The problem is that the parameter "sms_body" is not received on WhatsApp, though the contact is selected.
There is a way. Make sure that the contact you are providing must be passed as a string in intent without the prefix "+". Country code should be appended as a prefix to the phone number .
e.g.: '+918547264285' should be passed as '918547264285' . Here '91' in beginning is country code .
Note :Replace the 'YOUR_PHONE_NUMBER' with contact to which you want to send the message.
Here is the snippet :
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("YOUR_PHONE_NUMBER")+"#s.whatsapp.net");
startActivity(sendIntent);
Update:
The aforementioned hack cannot be used to add any particular message, so here is the new approach. Pass the user mobile in international format here without any brackets, dashes or plus sign. Example: If the user is of India and his mobile number is 94xxxxxxxx , then international format will be 9194xxxxxxxx. Don't miss appending country code as a prefix in mobile number.
private fun sendMsg(mobile: String, msg: String){
try {
val packageManager = requireContext().packageManager
val i = Intent(Intent.ACTION_VIEW)
val url =
"https://wa.me/$mobile" + "?text=" + URLEncoder.encode(msg, "utf-8")
i.setPackage("com.whatsapp")
i.data = Uri.parse(url)
if (i.resolveActivity(packageManager) != null) {
requireContext().startActivity(i)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
Note: This approach works only with contacts added in user's Whatsapp
account.
This new method, send message to a specific contact via whatsapp in Android. For more information look here
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_VIEW);
String url = "https://api.whatsapp.com/send?phone=" + number + "&text=" + path;
sendIntent.setData(Uri.parse(url));
activity.startActivity(sendIntent);here
I found the right way to do this and is just simple after you read this article: http://howdygeeks.com/send-whatsapp-message-unsaved-number-android/
phone and message are both String.
Source code:
try {
PackageManager packageManager = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
String url = "https://api.whatsapp.com/send?phone="+ phone +"&text=" + URLEncoder.encode(message, "UTF-8");
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
context.startActivity(i);
}
} catch (Exception e){
e.printStackTrace();
}
Enjoy!
Great hack Rishabh, thanks a lot, I was looking for this solution since last 3 years.
As per the Rishabh Maurya's answer above, I have implemented this code which is working fine for both text and image sharing on WhatsApp. I have published this in my android app, so if you want to see it live try my app Bill Book
Note that in both the cases it opens a whatsapp conversation (if toNumber exists in users whatsapp contact list), but user have to click send button to complete the action. That means it helps in skipping contact selection step.
For text messages
String toNumber = "+91 98765 43210"; // contains spaces.
toNumber = toNumber.replace("+", "").replace(" ", "");
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.putExtra("jid", toNumber + "#s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("text/plain");
startActivity(sendIntent);
For sharing images
String toNumber = "+91 98765 43210"; // contains spaces.
toNumber = toNumber.replace("+", "").replace(" ", "");
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
sendIntent.putExtra("jid", toNumber + "#s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("image/png");
context.startActivity(sendIntent);
Enjoy WhatsApping!
We can share/send message to whats app. Below is Sample code to send text message on Whats-app
Single user
private void shareToOneWhatsAppUser(String message) {
/**
* NOTE:
* Message is shared with only one user at a time. and to navigate back to main application user need to click back button
*/
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, message);
//Directly send to specific mobile number...
String smsNumber = "919900990099";//Number without with country code and without '+' prifix
whatsappIntent.putExtra("jid", smsNumber + "#s.whatsapp.net"); //phone number without "+" prefix
if (whatsappIntent.resolveActivity(getPackageManager()) == null) {
Toast.makeText(MainActivity.this, "Whatsapp not installed.", Toast.LENGTH_SHORT).show();
return;
}
startActivity(whatsappIntent);
}
Multiple user
private void shareToMultipleWhatsAppUser(String message) {
/**
* NOTE:
*
* If want to send same message to multiple users then have to select the user to whom you want to share the message & then click send.
* User navigate back to main Application once he/she select all desired persons and click send button.
* No need to click Back Button!
*/
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, message);
if (whatsappIntent.resolveActivity(getPackageManager()) == null) {
Toast.makeText(MainActivity.this, "Whatsapp not installed.", Toast.LENGTH_SHORT).show();
return;
}
startActivity(whatsappIntent);
}
One more way to achieve the same
private void shareDirecctToSingleWhatsAppUser(String message) {
/**
* NOTE:
* Message is shared with only one user at a time. and to navigate back to main application user need to click back button
*/
//Directly send to specific mobile number...
String smsNumber = "919900000000";//Intended user`s mobile number with country code & with out '+'
PackageManager packageManager = getPackageManager();
Intent i = new Intent(Intent.ACTION_VIEW);
try {
String url = "https://api.whatsapp.com/send?phone="+ smsNumber +"&text=" + URLEncoder.encode("Test Message!", "UTF-8");
i.setPackage("com.whatsapp");
i.setData(Uri.parse(url));
if (i.resolveActivity(packageManager) != null) {
startActivity(i);
}
} catch (Exception e){
e.printStackTrace();
}
}
you can use this code:
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("text/plain");
sendIntent.putExtra("jid", "9194******22" + "#s.whatsapp.net");// here 91 is country code
sendIntent.putExtra(Intent.EXTRA_TEXT, "Demo test message");
startActivity(sendIntent);
This is the best way to send Message through Whatsapp to specific number or unsaved number
private void openWhatsApp() {
String smsNumber = "252634651588";
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "#s.whatsapp.net");//phone number without "+" prefix
startActivity(sendIntent);
} else {
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
Toast.makeText(getContext(), "WhatsApp not Installed",
Toast.LENGTH_SHORT).show();
startActivity(goToMarket);
}
}
private boolean whatsappInstalledOrNot(String uri) {
PackageManager pm = Objects.requireNonNull(getContext()).getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
This is what works for me.
The parameter 'body' gets not red by the whatsapp app, use 'Intent.EXTRA_TEXT' instead.
By setting the 'phoneNumber' you specify the contact to open in whatsapp.
Intent sendIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("smsto:" + "" + phoneNumber + "?body=" + encodedMessage));
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
Uri mUri = Uri.parse("smsto:+90000900000");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("chat",true);
startActivity(Intent.createChooser(mIntent, "Share with"));
Works great to send message to specific contact on WhatsApp from my android app
Try this code
Uri uri = Uri.parse("smsto:" + "+6281122xxx");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.default_message_wa));
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
You can't put string directly on putExtra like this
i.putExtra(Intent.EXTRA_TEXT, "YOUR TEXT");
Change your code and get string from resource like this
i.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.default_message_wa));
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_VIEW);
String url ="https://wa.me/your number";
sendIntent.setData(Uri.parse(url));
startActivity(sendIntent);
Here's my way to do it (more here):
First, if you want to be sure you can send the message, you can check if the person has a WhatsApp account on the address book:
#RequiresPermission(permission.READ_CONTACTS)
public String getContactMimeTypeDataId(#NonNull Context context, String contactId, #NonNull String mimeType) {
if (TextUtils.isEmpty(mimeType) || !PermissionUtil.hasPermissions(context, Manifest.permission.READ_CONTACTS))
return null;
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, new String[]{Data._ID}, Data.MIMETYPE + "= ? AND "
+ ContactsContract.Data.CONTACT_ID + "= ?", new String[]{mimeType, contactId}, null);
if (cursor == null)
return null;
if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
String result = cursor.getString(cursor.getColumnIndex(Data._ID));
cursor.close();
return result;
}
and if all seem well, you open it as if it's from the web:
final String contactMimeTypeDataId = getContactMimeTypeDataId(context, contactId, "vnd.android.cursor.item/vnd.com.whatsapp.profile");
if (contactMimeTypeDataId != null) {
final String whatsAppPhoneNumber = PhoneNumberHelper.normalizePhone(phoneNumber);
String url = "https://api.whatsapp.com/send?phone="+ whatsAppPhoneNumber ;
intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
.setPackage("com.whatsapp");
startActivity(intent);
}
You can also check if WhatsApp is even installed before of all of this (or remove the setPackage and check if any app can handle the Intent) :
final PackageManager packageManager = context.getPackageManager();
final ApplicationInfo applicationInfo = packageManager.getApplicationInfo("com.whatsapp", 0);
if (applicationInfo == null)
return;
EDIT: about preparing the Intent with the Uri, I think this way is better:
#JvmStatic
fun prepareWhatsAppMessageIntent(normalizedPhoneNumber: String?, message: String? = null): Intent {
// example url: "https://api.whatsapp.com/send?phone=normalizedPhoneNumber&text=abc"
val builder = Uri.Builder().scheme("https").authority("api.whatsapp.com").path("send")
normalizedPhoneNumber?.let { builder.appendQueryParameter("phone", it) }
message?.let { builder.appendQueryParameter("text", it) }
return Intent(Intent.ACTION_VIEW, builder.build())
}
or alternative (based on here):
fun prepareWhatsAppMessageIntent(normalizedPhoneNumber: String?, message: String? = null): Intent {
// example url: "https://wa.me/normalizedPhoneNumber&text=abc"
val builder = Uri.Builder().scheme("https").authority("wa.me")
normalizedPhoneNumber?.let { builder.appendPath(it) }
message?.let { builder.appendQueryParameter("text", it) }
return Intent(Intent.ACTION_VIEW, builder.build())
}
Try using Intent.EXTRA_TEXT instead of sms_body as your extra key. Per WhatsApp's documentation, this is what you have to use.
An example from their website:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Their example uses Intent.ACTION_SEND instead of Intent.ACTION_SENDTO, so I'm not sure if WhatsApp even supports sending directly to a contact via the intent system. Some quick testing should let you determine that.

How to send Whatsapp message to new number

I' d like to send a whatsapp message by clicking on a button to a number that comes from the Android Activity (that in turn fetches from a server).
The number to which I have to send a new is NOT an existing contact on my phone.
I know how to open Whatsapp app from my app.
The following piece of code deals with opening whatsapp from an Adapter:
Intent sendIntent = new Intent();
sendIntent.setPackage("com.whatsapp");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
this code opens Whatsapp but I don't know how to pass it the number to which I have to send the message
Try this
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}

How to send message from Android app through Viber message

I want to write small Android app to send the message through Viber
to people whom are listed in my contact list. But I could not find
any sample code to do this task.
If you know how to do this task.
Please teach me.
Vonbk
If viber application is installed in your device, You can call an intent to share the text.
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = context.getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase(
Locale.getDefault()).contains("com.viber.voip")
|| info.activityInfo.name.toLowerCase(
Locale.getDefault()).contains("com.viber.voip")) {
share.putExtra(Intent.EXTRA_TEXT, "Your text to share");
share.setPackage(info.activityInfo.packageName);
found = true;
context.startActivity(Intent.createChooser(share, "Select"));
break;
}
}
if (!found) {
displayToast(context, "Install viber android application");
Uri marketUri = Uri.parse("market://details?id="
+ "com.viber.voip");
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
context.startActivity(marketIntent);
}
}
I am not sure it will work. But it will worth a shot.
You can also share with the plain intent which asks the user to select and share :
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text that will be shared.</p>"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));

Categories

Resources