Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt 5.x Android: Send e-mail with file attached
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Mobile and Embedded
12 Posts 5 Posters 6.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nando76
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    1
    • GianlucaG Offline
      GianlucaG Offline
      Gianluca
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nando76
          wrote on last edited by
          #4

          [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

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nando76
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nando76
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                1
                • L Offline
                  L Offline
                  ltr6
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    mr_wallyit
                    wrote on last edited by
                    #9

                    Hello,

                    I use QtSms to send e-mail with attached.

                    https://github.com/9316/QtEmail

                    It very useful.

                    My Best Regards
                    Pier

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      nando76
                      wrote on last edited by
                      #10

                      [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

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mr_wallyit
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          nando76
                          wrote on last edited by
                          #12

                          [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

                          1 Reply Last reply
                          2

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved