Qt 5.x Android: Send e-mail with file attached



  • hi friends,

    my app generates a csv file (export of user data).
    now i want to send this generated csv file via e-mail using the local os installed mail app.

    is there a general way in qt to do this? I need this feature for android and iOS.

    as i can see there exists a QMobility module with QMessageService class.
    but somehow i can not find it for Qt 5?

    does this stuff still exist in Qt5?

    greetings
    nando



  • No, there is no utility of Qt5 for sending email from Android and iOS.
    You have to use native code.


  • Lifetime Qt Champion

    Hi,

    You could try with "QDesktopServices":http://qt-project.org/doc/qt-5/qdesktopservices.html#openUrl however I don't know whether you can attache a file in the body part

    Hope it helps



  • [quote author="SGaist" date="1414616705"]Hi,

    You could try with "QDesktopServices":http://qt-project.org/doc/qt-5/qdesktopservices.html#openUrl however I don't know whether you can attache a file in the body part

    Hope it helps[/quote]

    Thank you very much.
    QDesktopService does automatically choose the mail programm from the OS and starts it. thats cool.
    Like you said, now i have to check if i can attach the file somehow.
    Will try it and then report.

    Thanks



  • Hi,

    i can not get it workin with mail attachment :(

    Maybe somebody has another idea how to transmit a file generated inside the app to the user desktop os?

    Greetings
    Nando


  • Lifetime Qt Champion

    Then you'll have to write some native code to do it



  • Hi,
    i now try to write native code.
    For android i want to call java code which then creates an Intent object to open the email program.

    I used the code from Notification example and modified it.

    Here is my java code:
    @

    public class Mailer extends org.qtproject.qt5.android.bindings.QtActivity {

    private static Mailer m_instance;
    
    public Mailer()
    {
        Log.d("Mailer::Mailer", "Constructor");
        m_instance = this;
    }
    
    public static void sendMail(String attachmentFilename)
    {
        Log.d("Mailer::sendMail", "java-code: sendMail(): 1 file=" + attachmentFilename);
        Log.d("Mailer::sendMail", "java-code: m_instance=" + m_instance);
        String f = "file://" + attachmentFilename;
        Uri uri = Uri.parse(f);
    
        try
        {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("message/rfc822");
            intent.putExtra(Intent.EXTRA_SUBJECT, "CSV export");
            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@gmail.com"});
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            Log.d("Mailer::sendMail", "java-code: sendMail(): 2");
            Intent mailer = Intent.createChooser(intent, null);
            Log.d("Mailer::sendMail", "java-code: sendMail(): 3");
            if(mailer == null)
            {
                Log.e("Mailer::sendMail", "Couldn't get Mail Intent");
                return;
            }
            Log.d("Mailer", "java-code: mailer    =" + mailer);
            Log.d("Mailer", "java-code: m_instance=" + m_instance);
    
            m_instance.getApplicationContext().startActivity(mailer);
        }
        catch (android.content.ActivityNotFoundException ex)
        {
            Log.e("Mailer", "catched android.content.ActivityNotFoundException while starting activity");
            ex.printStackTrace();
        }
    
        Log.d("Mailer", "java-code: notify(): END");
    }
    

    }
    @

    And here the C++ code where i call the stuff in java:
    @
    #ifdef Q_OS_ANDROID
    QAndroidJniObject javaFilenameStr = QAndroidJniObject::fromString(exportFilename);
    qDebug() << "call QAndroidJniObject::callStaticMethod";
    QAndroidJniObject::callStaticMethod<void>(
    "de/myApp/Mailer",
    "sendMail",
    "(Ljava/lang/String;)V", javaFilenameStr.object<jstring>());
    #endif
    @

    The sendMail method gets called, that is fine, but m_instance stays null...

    Somehow the constructor gets not called but it should like in the notification example.

    I don't know where the static object is created. I thought the static member m_instance creates and then represents the object?

    In the Notification example it works, but i don't know why / where the different is in object creation.

    Maybe somebody see the problem here?

    Greetings
    Nando



  • What does your AndroidManifest.xml look like? I think in the <activity> you have to set android:name="de.myApp.Mailer"



  • Hello,

    I use QtSms to send e-mail with attached.

    https://github.com/9316/QtEmail

    It very useful.

    My Best Regards
    Pier



  • [quote author="mr_wallyit" date="1415264131"]Hello,

    I use QtSms to send e-mail with attached.

    https://github.com/9316/QtEmail

    It very useful.

    My Best Regards
    Pier[/quote]

    Hi Pier,
    thank you for this information.
    The problem is that i do not want that the user must enter smtp server data. It is a bit too complicated for a "normal" user.
    But i will take a look at it and maybe it will be my backup solution.
    Thank you



  • Hello,

    I had a some problem, but I have inserted my account google on source code.
    In fact with account gmail we can use smtp of google, so user sends the email without problem.

    If you use Android user has an account gmail, so you can use it.

    My Best Regards
    Pier



  • [quote author="ltr6" date="1415182621"]What does your AndroidManifest.xml look like? I think in the <activity> you have to set android:name="de.myApp.Mailer"[/quote]

    You are the man ;-)
    Thank you.
    That was exactly the problem.
    I just added android:name="de.myApp.Mailer" in <activity> of the AndroidManifest.xml
    and now the Mailer java object gets instantiated like expected.
    To allow another app (some mailing app) to access the file to attach we must make sure the file is not in the sandboxed app environment. I use for file creation:

    @
    QString tempDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
    @

    Important: Need to rebuild project

    Thank you


Log in to reply
 

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