Quantcast
Channel: Planet Object Pascal
Viewing all articles
Browse latest Browse all 1725

Daniel Magin's Logfile: Delphi XE5 Android Send Mails with Attachments over Android Mail Client

$
0
0

Customer asked my how to send with a Delphi XE5 Android Application a Mail from the Delphi App with attachment.

Why not to share?

Here is the code:

  1. unit u_frmMain;
  2.  
  3. interface
  4.  
  5. uses
  6.   System.SysUtils, System.Types, System.UITypes, System.Classes,
  7.   System.Variants,
  8.   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     Button1: TButton;
  13.     procedure Button1Click(Sender: TObject);
  14.   private
  15.     procedure CreateEmail(const Recipient, Subject, Content,
  16.       Attachment:string);
  17.     { Private declarations }
  18.   public
  19.     { Public declarations }
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. uses
  28.   Androidapi.JNI.GraphicsContentViewText,
  29.   Androidapi.JNIBridge,
  30.   Androidapi.JNI.JavaTypes,
  31.   FMX.Helpers.Android,
  32.   Androidapi.JNI.Net,
  33.   Androidapi.JNI.Os,
  34.   Androidapi.IOUtils;
  35.  
  36. {$R *.fmx}
  37.  
  38. procedure TForm1.CreateEmail(const Recipient, Subject, Content,
  39.   Attachment:string);
  40. var
  41.   Intent: JIntent;
  42.   Uri: Jnet_Uri;
  43.   AttachmentFile: JFile;
  44. begin
  45.   Intent := TJIntent.Create;
  46.   Intent.setAction(TJIntent.JavaClass.ACTION_Send);
  47.   Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
  48.   Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, StringToJString(Recipient));
  49.   Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(Subject));
  50.   Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(Content));
  51.   AttachmentFile := SharedActivity.getExternalFilesDir
  52.     (StringToJString(Attachment));
  53.  
  54.   Uri := TJnet_Uri.JavaClass.fromFile(AttachmentFile);
  55.  
  56.   Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,
  57.     TJParcelable.Wrap((Uri as ILocalObject).GetObjectID));
  58.  
  59.   Intent.setType(StringToJString('vnd.android.cursor.dir/email'));
  60.  
  61.   SharedActivity.startActivity(Intent);
  62. end;
  63.  
  64. procedure TForm1.Button1Click(Sender: TObject);
  65. var
  66.   sl: TStringList;
  67.   sFileName:string;
  68. begin
  69.   sFileName := Androidapi.IOUtils.getExternalFilesDir+ PathDelim +'Test.txt';
  70.   sl := TStringList.Create;
  71.   try
  72.     sl.Add('TestContent');
  73.     sl.SaveToFile(sFileName);
  74.   finally
  75.     FreeAndNil(sl);// Arc eigentlich unnötig
  76.   end;
  77.   CreateEmail('aaaaa@bbbbb.com','TestFromDelphi','Dödeldiedeidiedödeldiemöp','Test.txt');
  78. end;
  79.  
  80. end.

Viewing all articles
Browse latest Browse all 1725

Trending Articles