-1

I wrote some application ( marshmallow sdk ) that in some action i need to send mail with some file attached using the default device account to well know user.

When i trying to use my sending action ( code attached ) i getting some dialog that tell me to choose the application that i want to use for this shared this file attached.

I want to skip this dialog somehow and just make this sending action be done without give the user that use this application to choose the gmail and press the 'send' button.

( like sending the mail in the background )

The code:

 public void SendToMail(String filePath)
{
    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"somemailAddress@gmail.com"});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Reminder");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "Null");

    File file = new File(filePath);
    if (!file.exists() || !file.canRead()) {
        return;
    }

    Uri uri = Uri.fromFile(file);
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);


    final PackageManager pm = _context.getApplicationContext().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);

    _context.startActivity(emailIntent);
}
Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • possible duplicate of http://stackoverflow.com/questions/5789545/how-to-direct-open-gmail-mail-composer-in-android/5789643#5789643 – lal Jul 25 '16 at 05:04
  • Possible duplicate of [How can I send emails from my Android application?](http://stackoverflow.com/questions/2197741/how-can-i-send-emails-from-my-android-application) – Tanishq Sharma Jul 25 '16 at 05:04
  • 1
    And if the user is using Inbox rather than Gmail? Apps != accounts. – ianhanniballake Jul 25 '16 at 05:09
  • @lal and @ Tanishq Sharma - the link that you gave me is not solving the problem because the user still need to press on the 'send' button. – Yanshof Jul 25 '16 at 05:20
  • 1
    If you want to send a direct mail, you may need to use our own server side email sending mechanism. – lal Jul 25 '16 at 05:24
  • @ lal - this way is well know to me. but i looking some other way. – Yanshof Jul 25 '16 at 05:29
  • Possible duplicate of [Sending email using standard gmail app without chooser](http://stackoverflow.com/questions/30314321/sending-email-using-standard-gmail-app-without-chooser) – Ethan Jul 25 '16 at 14:51

1 Answers1

0

You can do this from android using javamail api You can try this link Send email in android using JavaMail API with smtp but without SSL

But after some time gmail may gives error on your account because your location may vary as user from different countries access your gmail using your application.

Community
  • 1
  • 1
Krutik
  • 732
  • 6
  • 19