Send an Email with an Attachment in iOS, Android, and Windows using RAD Studio - Updated 8/10/2021
We are updating our solution for this topic as some things had become outdated due to changes in Android and RAD Studio(August 10th, 2021). The solution provided here does not require any 3rd party components.
Today, I am going to share how to send emails with attachments with FireMonkey for both iOS, Android, and Windows devices.
Sending Email (with Attachment)
We have provided the file wwEmailWithAttachment.pas which you can use to send an email with an attachment. This code is compatible and has been tested with RAD Studio 10.4 Sydney. It also includes a complete project which you can compile and run. Please see below for the extra steps when deploying to an Android device
This project contains a unit named wwEmailWithAttachment.pas which defines the method method wwEmail.
procedure wwEmail(
Recipients: Array of String;
ccRecipients: Array of String;
bccRecipients: Array of String;
Subject, Content,
AttachmentPath: string;
mimeTypeStr: string = '');
To use this method, add the unit wwEmailWithAttachment to your form’s uses clause (If you have FirePower installed, the use the unit name FMX.wwEmailWithAttachment instead), and then call the procedure as in the following.
wwEmail(['roywoll@gmail.com', 'royswoll@yahoo.com'],
[], [], 'Subject', 'Content', fileName);
Android Additional Steps
When building with more recent Android SDK versions you will need to give your application the ability to update URI paths. The steps are the following.
a) With RAD Studio 10.4 or later
From the IDE, click Project | Options | Application | Entitlement List and then set Secure File Sharing to true. This will automatically enable file sharing so that your attachment can be created and opened by Android sharing
b) With RAD Studio 10.3 and earlier
1. Edit your project AndroidManifest.template.xml file (located in your main project directory) and insert the following text:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="%package%.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
2. Create the following file named file_provider_paths.xml and put it in your project's main directory
<?xml version="1.0" encoding="utf-8"?>
<paths
xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
3. Within the Delphi IDE, add the file_provider_paths.xml to your deployment for Android
After adding the file, edit the Remote Path location so that it contains the value res\xml\
Please refer to our demo project PMailAttachment for a complete example of this as it has implemented all the above steps.
Windows
The method also works in Windows if Outlook is installed.
FirePower X Email Support
FirePower X comes with the unit FMX.wwEmailWithAttachment, and additionally includes the necessary files for C++ Builder support. If using FirePower X with Delphi, you simply need to add the unit FMX.wwEmailWithAttachment to your form's uses clause. If using C++ Builder, include the header file.
#include "FMX.wwEmailWithAttachment.hpp"
For a complete example of usage with C++ Builder, see the included demo in the FirePower\13.0\demos\21.0\EmailWithAttachmentCPlus directory
Editors Note: Please note that this method has no other dependencies outside of RAD Studio. It currently does not support OSX.