如何使用默认的Android电子邮件应用程序发送带有附件的电子邮件-Delphi XE7 [英] How to send email with attachment using default Android email app - Delphi XE7
问题描述
使用下面的代码,我在另一篇文章中找到了,该电子邮件似乎已准备好与附件一起发送,但是收到电子邮件后,便没有附件.另外,必须手动输入电子邮件地址,CreateEmail语句不会填充该电子邮件地址.我是从Gmail帐户发送的.有人帮忙吗?
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;
推荐答案
Intent.EXTRA_EMAIL
是已记录,因为它期望使用字符串值的数组,但是您正在传递的是单个字符串.
Intent.EXTRA_EMAIL
is documented as expecting an array of string values, but you are passing it a single string instead.
您还没有使用 type
参数指定要查找的文件夹的类型(MUSIC
,PODCASTS
,PICTURES
等),然后返回表示该文件夹.然后,您可以根据需要将文件名附加到该文件夹的路径.但是,在这种情况下,您的Attachment
字符串包含您要附加的实际文件的完整路径,因此您根本不应该调用getExternalFilesDir()
.而是直接从路径中创建JFile
.
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.
尝试一下:
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中发送电子邮件的更多详细信息:
Read this article for more details about sending emails in Android:
在Delphi XE5 Android应用中启动活动和处理结果|发送电子邮件
这篇关于如何使用默认的Android电子邮件应用程序发送带有附件的电子邮件-Delphi XE7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!