AMIT
AMIT

Reputation: 400

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();

    }

}

Upvotes: 1

Views: 1333

Answers (2)

ngrashia
ngrashia

Reputation: 9904

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

Upvotes: 1

Mukesh
Mukesh

Reputation: 515

No, you cant send mail from gmail without user intervention, Doing so will raise security issue. Only thing you can call that app via intent as you did above. Or else you can use any java mail API for sending mails.

Upvotes: 1

Related Questions