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 on Android: how to send email with attachment

Qt on Android: how to send email with attachment

Scheduled Pinned Locked Moved Mobile and Embedded
7 Posts 2 Posters 6.9k Views
  • 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.
  • G Offline
    G Offline
    GGILOYAN
    wrote on last edited by
    #1

    Hi everyone.
    Please help!

    How can I send email with QImage as picture attachment in Qt5.2 or QT5.1 Android ?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MartynW
      wrote on last edited by
      #2

      If you don't want an attachment then you can simply use "mailto:" in an OS independent way. Otherwise, you either need to implement an SMTP client to log into a mail server, or if you want to use any installed Android email app you have to use Qt Android Extras module and create an Android intent for the email in java. I got proof of concept (sending a file attachment) by hacking the 'notification' example in Qt Android Extras thus:

      @ public static void notify(String s)
      {
      Context context = m_instance.getApplicationContext();
      String mess = email(context,
      "yourname@yourdomain.com", "", "Test subject", "body text.", new ArrayList<String>());
      }
      public static String email(final Context context, String emailTo, String emailCC,
      String subject, String emailText, List<String> filePaths)
      {
      try {
      //Send the email
      Intent mailIntent = new Intent(Intent.ACTION_SEND);
      mailIntent.setType("message/rfc822"); // Lists mail only? No
      mailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{emailTo});
      mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Test Email");
      mailIntent.putExtra(Intent.EXTRA_TEXT , "Hi! This is a test!");
      //Deal with the attached report
      File attachment = new File("/storage/emulated/0/Download/SearchResults.pdf");

              if (!attachment.exists() || !attachment.canRead())
              {
                  m_instance.runOnUiThread(new Runnable()
                  {
                     public void run()
                     {
                        try
                        {
                            Toast.makeText(context,
                                           "Attachment Error",
                                           Toast.LENGTH_SHORT).show();
                            System.out.println("ATTACHMENT ERROR");
                        }
                        catch (java.lang.Exception ex)
                        {
                            System.out.println("***** toast Exception: " + ex.getMessage());
                        }
                      }
                   });
              }
              else
              {
                  Uri uri = Uri.fromFile&#40;attachment&#41;;
                  mailIntent.putExtra(Intent.EXTRA_STREAM, uri);
                  System.out.println("***** found attachment uri");
              }
      
              //Send, if valid!
              try {
                 Intent i = Intent.createChooser(mailIntent, "Send mail...");
                 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 context.startActivity(i);
              } catch (android.content.ActivityNotFoundException ex) {
                             System.out.println("***** There are no email clients installed.");
              }
              catch (java.lang.RuntimeException ex)
              {
                  System.out.println("***** email RuntimeException: " + ex.getMessage());
                  return ex.getMessage();
              }
              }
              catch (java.lang.Exception ex)
              {
                  System.out.println("***** toast Exception: " + ex.getMessage());
              }
          }
          return "email Ok";
      }
      

      }
      @

      Hope it helps!

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GGILOYAN
        wrote on last edited by
        #3

        Thank you but it's writed by Java ..

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MartynW
          wrote on last edited by
          #4

          Yes, but unfortunately QT does not provide the C++ functionality to access the operating system to list the possible email apps installed (or skype etc, that can send the attachment to someone) and then provide a method to pass the user-selected app the attachment etc.

          Qt does provide an api to allow you to call a Java routine and that java routine can then access the Android APIs directly to do the business. Not nice but I believe it is the only possible way to send an email with an attachment using an already installed email app. The only other way is to write/use an SMTP client to connect to a mail server using TCP/IP and send an email that way, but then you need to have to hand the email server ip address, login, password, etc. which you will have to configure into your app. This excellent smtp client works very well for me: "https://github.com/bluetiger9/SmtpClient-for-Qt":https://github.com/bluetiger9/SmtpClient-for-Qt

          There is a simple Qt example that shows how to call java and that is what I used to get started with the java code in my previous post. Its the Qt Extras package example 'notification'.

          There just isn't any easy way to do what you want unfortunately. Good luck.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GGILOYAN
            wrote on last edited by
            #5

            Thank you very much., I tryed that example and it has works well. Using SMTP Client I sent email with attachment. But are all andorid mobiles supported SMTP ?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MartynW
              wrote on last edited by
              #6

              Hey, great that is does what you want, but either I don't understand your question or you misunderstand SMTP. The SMTP client simply makes a network connection to a remote SMTP server using its ip address, logs in, does what it needs to and then logs out. So the client works for any mobile (or PC), Android or otherwise, as long as the mobile has TCP/IP connectivity to the SMTP server ip address. You should be fine.

              The only downside is that if a user has already set up an email app and entered all the mail server details once, your app cannot make use of that and the user must enter the detail again into your app. But of course you app will work even without an email app being installed - if that ever happens! It also means that if the user wants to use their contacts list to email or cc to one or more contacts, then that happens naturally in the existing email app by using the Android-specific java stuff, but using the generic SMTP client it has become your problem. Decisions, decisions:)

              1 Reply Last reply
              0
              • G Offline
                G Offline
                GGILOYAN
                wrote on last edited by
                #7

                Thank you for info!

                1 Reply Last reply
                0

                • Login

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