簡體   English   中英

如何使用默認的Android電子郵件應用程序發送帶有附件的電子郵件-Delphi XE7

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

使用下面在另一篇文章中找到的代碼,該電子郵件似乎可以隨附件一起發送,但是當收到電子郵件時,沒有附件。 另外,必須手動輸入電子郵件地址,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;

Intent.EXTRA_EMAIL記錄為期望字符串值的數組 ,但是您將其傳遞為單個字符串。

您也沒有正確使用SharedActivity.getExternalFilesDir() type參數指定要查找的文件夾類型MUSICPODCASTSPICTURES等),然后返回代表該文件夾JFile 然后,您可以根據需要將文件名附加到該文件夾​​的路徑。 但是,在這種情況下, Attachment字符串包含您要附加​​的實際文件的完整路徑,因此您根本不應調用getExternalFilesDir() JFileJFile創建路徑的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中發送電子郵件的更多詳細信息:

在Delphi XE5 Android應用程序中啟動活動和處理結果| 發送郵件

這是多個附件的工作代碼。 在柏林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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM