Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiFiremonkey

Send Emails With Attachments From Delphi XE5 On Android

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Android Email Attachment Delphi Firemonkey | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSDo you need to send emails with attachments over your Android Mail Client using Delphi XE5? Daniel Magin, on his Developer Experts blog, has posted his code on how to do this.  Essentially you just need to call Daniel’s “CreateEmail” procedure passing the Recipient, Subject, Content, and Attachment  string parameters. If your attachment file does not already exist, Daniel shows how you might create one.
procedure TForm1.Button1Click(Sender: TObject);
var
  sl: TStringList;
  sFileName: string;
begin
  sFileName := Androidapi.IOUtils.getExternalFilesDir + PathDelim + 'Test.txt';
  sl := TStringList.Create;
  try
    sl.Add('TestContent');
    sl.SaveToFile(sFileName);
  finally
    FreeAndNil(sl); // Arc eigentlich unnötig
  end;
  CreateEmail('[email protected]', 'TestFromDelphi', 'Dödeldiedeidiedödeldiemöp','Test.txt');
end;

Learn how to send mail with attachments with your Delphi XE5 Android Application.

Update: Link appears to be down. Try this StackOverflow answer.
procedure TForm1.CreateEmail(const Recipient, Subject, Content, Attachment: string);
var
JRecipient: TJavaObjectArray<JString>;
Intent: JIntent;
Uri: Jnet_Uri;
AttachmentFile: JFile;
begin
JRecipient := TJavaObjectArray<JString>.Create(1);
JRecipient.Items[0] := StringToJString(Recipient);

Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_SEND);
Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, JRecipient);
Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(Subject));
Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(Content));

if Attachment <> '' then
begin
AttachmentFile := TJFile.JavaClass.init(StringToJString(Attachment));
Uri := TJnet_Uri.JavaClass.fromFile(AttachmentFile);
Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM, TJParcelable.Wrap((Uri as ILocalObject).GetObjectID));
end;

Intent.setType(StringToJString('vnd.android.cursor.dir/email'));

SharedActivity.startActivity(Intent);
end;

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts
DelphiDemoFiremonkeyLinuxOSXShowcaseWindows

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging

DelphiFiremonkeyShowcaseUtilityWindows

Unleashing Creativity With Song Writer AI: A Deep Dive

DelphiFiremonkeyShowcaseWindows

How To Build Stable Diffusion Text To Image Prompts

AndroidC++BuilderDelphiFiremonkeyIOSOSXWindows

FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

3 Comments

Leave a Reply