How to send email with attachment using default Android email app - Delphi XE7

两盒软妹~` 提交于 2020-01-02 18:07:12

问题


Using code below which I found on another post, the email appears ready to send with the attachment, but when email is received, there is no attachment. Also, the email address has to be manually entered, it is not populated by the CreateEmail statement. I am sending from a gmail account. Anyone help please?

procedure TForm1.CreateEmail(const Recipient, Subject, Content,
 Attachment: string);
var
 Intent: JIntent;
 Uri: Jnet_Uri;
 AttachmentFile: JFile;
begin
 Intent := TJIntent.Create;
 Intent.setAction(TJIntent.JavaClass.ACTION_SEND);
 Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
 Intent.putExtra(TJIntent.JavaClass.EXTRA_EMAIL, StringToJString(Recipient));
 Intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString(Subject));
 Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString(Content));
 AttachmentFile := SharedActivity.getExternalFilesDir
   (StringToJString(Attachment));

 Uri := TJnet_Uri.JavaClass.fromFile(AttachmentFile);

 Intent.putExtra(TJIntent.JavaClass.EXTRA_STREAM,
   TJParcelable.Wrap((Uri as ILocalObject).GetObjectID));

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

 SharedActivity.startActivity(Intent);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 CreateEmail('xxx@shaw.ca', 'Test Results', Memo1.Lines.text,'/sdcard/Download/Demo.pdf');
end;

回答1:


Intent.EXTRA_EMAIL is documented as expecting an array of string values, but you are passing it a single string instead.

You are also not using SharedActivity.getExternalFilesDir() correctly. Its type parameter specifies the type of folder you want to lookup (MUSIC, PODCASTS, PICTURES, etc), and then it returns a JFile that represents that folder. You can then append a filename to the path of that folder as needed. However, in this case, your Attachment string contains a full path to the actual file that you want to attach, so you should not be calling getExternalFilesDir() at all. Create a JFile from the path as-is instead.

Try this:

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;

Read this article for more details about sending emails in Android:

Launching activities and handling results in Delphi XE5 Android apps | Sending an email




回答2:


Here is working code for multiple attachments. Works in 10.1 Berlin.

procedure TForm1.ItemShare;
var
  chooserIntent, Intent: JIntent;
  Uri: Jnet_Uri;
  Uris: JArrayList;
  AttachmentFile: JFile;
begin
  {$IFDEF ANDROID}
    intent := TJIntent.Create;    
    intent.setAction(TJIntent.JavaClass.ACTION_SEND_MULTIPLE);
    intent.setType(StringToJString('text/*'));
    intent.putExtra(TJIntent.JavaClass.EXTRA_SUBJECT, StringToJString('Email header'));

    Uris:= TJArrayList.Create;
    while i<condition
    begin
      AttachmentFile := TJFile.JavaClass.init(StringToJString('filename'));
      Uri := TJnet_Uri.JavaClass.fromFile(AttachmentFile);
      Uris.add(i,Uri);
      inc(i);
    end;   

    Intent.putParcelableArrayListExtra(TJIntent.JavaClass.EXTRA_STREAM, Uris);    
    Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT, StringToJString('body'));   
    chooserIntent := TJIntent.JavaClass.createChooser(Intent, StrToJCharSequence('Share using'));
    TAndroidHelper.Activity.startActivityForResult(chooserIntent, 0);
  {$ENDIF}
end;


来源:https://stackoverflow.com/questions/28956563/how-to-send-email-with-attachment-using-default-android-email-app-delphi-xe7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!