Search code examples
androidemailandroid-intentgmailjakarta-mail

How to send mail through gmail directly with out opening gmail compose page


By using this code i am able to get the gmail compose page directly, but i want to send this mail directly with out coming on gmail page. means when i click on my send button of activity it should directly send the mail to the perticular reciept with out goind on gmail compose page.

protected void sendEmail() {

    String[] recipients = { recieverId.getText().toString() };
    Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));

    email.setType("message/rfc822");

    email.putExtra(Intent.EXTRA_EMAIL, recipients);
    email.putExtra(Intent.EXTRA_SUBJECT, mailSubject.getText().toString());
    email.putExtra(Intent.EXTRA_TEXT, mailBody.getText().toString());

    final PackageManager pm = getPackageManager();
    final List<ResolveInfo> matches = pm.queryIntentActivities(email, 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)
      email.setClassName(best.activityInfo.packageName, best.activityInfo.name);

    try {

    startActivity(email);
    } catch (android.content.ActivityNotFoundException ex) {

        Toast.makeText(getApplicationContext(),
                "No email client installed.", Toast.LENGTH_LONG).show();

    }

}

Solution

  • If sending via app, user intervention is required.

    Without user intervention, you can send as follows:

    1. Send email from client apk. Here mail.jar, activation.jar is required to send java email. If these jars are added, it might increase the APK Size. Link

    2. Alternatively, You can use a web-service at the server side code, which will use the same mail.jar and activation.jar to send email. You can call the web-service via asynctask and send email. Refer same link