我想在邮件中附上.vcf文件并通过邮件发送。但邮件是在没有附件的地址上收到的。我使用了下面的代码,但代码是这个,我不知道我错在哪里。
try {
String filelocation="/mnt/sdcard/contacts_sid.vcf";
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setData(Uri.parse("mailto:"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
activity.finish();
} catch(Exception e) {
System.out.println("is exception raises during sending mail"+e);
}
答案 0 :(得分:82)
使用以下代码在电子邮件中发送文件。
String filename="contacts_sid.vcf";
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd@gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
答案 1 :(得分:8)
Folder_name是手机内部存储中的文件名。 (实际上是EXTERNAL_STORAGE)。 file_name是您要发送的文件的名称。
private void ShareViaEmail(String folder_name, String file_name) {
try {
File Root= Environment.getExternalStorageDirectory();
String filelocation=Root.getAbsolutePath() + folder_name + "/" + file_name;
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
String message="File to be shared is " + file_name + ".";
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setData(Uri.parse("mailto:xyz@gmail.com"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} catch(Exception e) {
System.out.println("is exception raises during sending mail"+e);
}
}
答案 2 :(得分:4)
官方Android site上的例子为我工作。 所有需要它添加
startActivity(Intent.createChooser(emailIntent , "Send email..."));
像在Agarwal的回答中所做的那样
答案 3 :(得分:2)
SENDTO不支持附件。我已使用Provider添加了我的答案来读取文件信息。它在Kotlin。
fun shareFile(context: Context, filePath: File?, fileShareInfo: FileShareInfo) {
val intentFileShare = Intent(Intent.ACTION_SEND)
if (filePath!!.exists()) {
intentFileShare.type = fileShareInfo.fileType
val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", filePath)
intentFileShare.putExtra(Intent.EXTRA_STREAM, uri)
fileShareInfo.recipients?.let {
intentFileShare.putExtra(Intent.EXTRA_EMAIL, fileShareInfo.recipients)
}
intentFileShare.putExtra(Intent.EXTRA_SUBJECT, fileShareInfo.shareSubjectText)
fileShareInfo.shareExtraText?.let {
intentFileShare.putExtra(Intent.EXTRA_TEXT, AppViewUtil.fromHtml(fileShareInfo.shareExtraText!!))
}
try {
ContextCompat.startActivity(context, Intent.createChooser(intentFileShare, fileShareInfo.shareTitle), null)
} catch (e: ActivityNotFoundException) {
Toast.makeText(context, context.getString(R.string.sharing_no_app_found), Toast.LENGTH_LONG).show()
}
}
}
答案 4 :(得分:0)
我在Kotlin中编写了一个扩展程序,用于发送带有多个附件的电子邮件。我希望它对某人有用。
fun AppCompatActivity.sendEmail(subject: String, content: String, email: String, fileAttachments: List<String> = emptyList()) {
val emailIntent = Intent(Intent.ACTION_SEND_MULTIPLE).apply {
type = "text/html"
putExtra(Intent.EXTRA_SUBJECT, subject)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra(Intent.EXTRA_TEXT, content)
putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
// Configure attachments
val attachments = fileAttachments.map { File(it) }.filter { it.exists() && !it.isDirectory }.map {
FileProvider.getUriForFile(baseContext, "${BuildConfig.APPLICATION_ID}.fileprovider", it)
}.toList()
if(attachments.isNotEmpty())
putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(attachments))
}
if (emailIntent.resolveActivity(packageManager) != null)
startActivity(Intent.createChooser(emailIntent, "Chooser Mail Client"))
}