Qt on Android: how to send email with attachment
-
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(attachment); 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!
-
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.
-
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:)