Do 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;
I think the link is dead. any code for this?
Found:
https://stackoverflow.com/questions/28956563/how-to-send-email-with-attachment-using-default-android-email-app-delphi-xe7
Hi !
I have used CreateEmail but get under debug java.langIlligalargumentexception=serviceregistred=Null.
I use Delphi XE7
Ilkka