Send email using default mail client /gmail in Android

  • Hi All,

    In my android app, I want to email some data using the defualt email client (most often gmail). I found that I can use "mailto:" approach if Im not attaching anything. QDesktopServices::openURL(QURL("mailto:?subject=Test&body= "test"))

    But I need to attach a file, and I found that its possible to do this using QMessage and QMessageServices classes in Qt Mobility. Im working on Qt 5.3 and it seems like some modules in Qt Mobility are integrated into the Qt Core itself.

    My Questions are :

    • Has QMessage and QMessageSErvices Modules being integrated in the Qt Core? If so, under what name?
    • Can I manually install Qt Mobility with Qt 5.3?
    • Any other approach that I can use for my task?

    Thank you very much for your support... I seem to be going round and round in circles...

  • Hi.

    You can write the method for sending email in your Java class and call it from C++ code.

    For example:

    public void SendEmail(String attachment)
    File file = new File(attachment);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] {""});
    intent.putExtra(Intent.EXTRA_SUBJECT, "Some subject");
    intent.putExtra(Intent.EXTRA_TEXT, "Hello, World!");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    startActivity(Intent.createChooser(intent, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
    android.widget.Toast.makeText(m_instance, "There are no email clients installed.", android.widget.Toast.LENGTH_SHORT).show();

    Calling SendEmail function from C++ code:
    // Get activity
    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
    // Path to attachment
    QAndroidJniObject jpath = QAndroidJniObject::fromString(QString(path));
    // Calling method SendEmail
    activity.callMethod<void>("SendEmail", "(Ljava/lang/String;)V", jpath.object<jstring>());

  • Please take a look also at this thread:

    "Send Mail on Android":


  • Thank you very much Khachatur and nando76. This solved my problem!


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.