如何使用默认的Android电子邮件应用程序发送带附件的电子邮件 - Delphi XE7

时间:2015-03-10 05:09:46

标签: android delphi android-intent firemonkey delphi-xe7

使用我在another post上找到的代码,电子邮件似乎已准备好与附件一起发送,但收到电子邮件时,没有附件。此外,必须手动输入电子邮件地址,CreateEmail语句不会填充该地址。我是从一个Gmail帐户发送的。有人帮忙吗?

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;

2 个答案:

答案 0 :(得分:2)

Intent.EXTRA_EMAILdocumented,因为期待数组字符串值,但您传递的是单个字符串。

您还没有正确使用SharedActivity.getExternalFilesDir()。其type参数指定要查找的文件夹类型MUSICPODCASTSPICTURES等),然后返回表示文件夹JFile。然后,您可以根据需要将文件名附加到该文件夹​​的路径。但是,在这种情况下,您的Attachment字符串包含要附加的实际文件的完整路径,因此您根本不应该调用getExternalFilesDir()。而是按原样从路径创建JFile

试试这个:

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;

阅读本文,了解有关在Android中发送电子邮件的更多详细信息:

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

答案 1 :(得分:1)

以下是多个附件的工作代码。适用于10.1柏林。

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;